0

I'm new to CodeIgniter and I'm trying to figure out why the standard methods that I have seen both on the codeigniter site and here on SO aren't working for me to get rid of index.php from the url in my project.

I'm running CentOs, and the path to my project looks like: /var/www/html/myproject

I have made sure that mod_rewrite is enabled in httpd.conf by checking via phpinfo()

My .htacess file looks like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /myproject/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

my config.php looks like this (I have also tried PATH_INFO and REQUEST_URI for uri_protocol):

$config['base_url'] = 'http://localhost/myproject/';
$config['index_page']   = '';
$config['uri_protocol'] = 'AUTO';

My routes.php looks like this:

$route['default_controller'] = "site";
$route['404_override'] = '';
$route['site/price/(.*)'] = "site/price/$1";

The signature for the site controller's price action is this:

public function price($a, $b, $c) {
    //code
}

When I go to

http://localhost/myproject/index.php/site/price/32137/2/1 

the price function is reached successfully, but when I try

http://localhost/myproject/site/price/32137/2/1 

I get an error page saying

The requested URL /myproject/site/price/32137/2/1 was not found on this server.

Am I missing something? Thanks in advance

EDIT: If it's any help, I can get to my project's site/index method with just the url: localhost/myproject but none of the other actions appear reachable.

  • Duplicate. [http://stackoverflow.com/questions/5155333/remove-index-php-from-url-codeigniter-2](http://stackoverflow.com/questions/5155333/remove-index-php-from-url-codeigniter-2) – Derek Nutile Jan 05 '14 at 19:36
  • it isn't a duplicate. It's similar, but nothing in that thread worked for me. – user3163245 Jan 05 '14 at 23:00

4 Answers4

0

Use the exact .htaccess mod_rewrite config provided in the docs for Codeigniter

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Works for me every time.

Zarathuztra
  • 3,215
  • 1
  • 20
  • 34
  • I tried that initially, and again just now to make sure I hadn't missed anything. It still not working. I tried both /index.php and /myproject/index.php in that last line – user3163245 Jan 05 '14 at 22:52
  • Hmmm that's an odd one. This one is going to keep me up tonight. Been playing with CI for years and never tried putting it in a subdir like that. Here is someone who was trying to do the same, let me know if it helps: http://ellislab.com/forums/viewthread/237589/#1058216 – Zarathuztra Jan 06 '14 at 02:57
0
RewriteEngine On
RewriteBase /myproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
0

There are 2 parts to removing index.php from the url:

1) Tell .htaccess to route around index.php. Something like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

2) Edit your system/cms/config/config.php configuration file and update this setting from:

$config['index_page'] = 'index.php';

to

$config['index_page'] = '';

Here's a sample .htaccess file I use:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -Indexes
    RewriteEngine On

    # Keep people out of codeigniter directory and Mercurial data
    RedirectMatch 403 ^/(/codeigniter|\.git|\.hg).*$

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^codeigniter.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 public/index.php
</IfModule>
Derek Nutile
  • 211
  • 3
  • 10
  • As I said in the original post, I did change the $config['index_page'] var in config.php to an empty string, and the content I posted from my .htaccess file doesn't look different from the one you posted in a meaningful way. I tried it anyway, but it doesn't work – user3163245 Jan 06 '14 at 00:28
0

how about this

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Vainglory07
  • 5,073
  • 10
  • 43
  • 77