Sven van Zoelen
Sven van Zoelen

Reputation: 7229

Rewriterule for CodeIgniter not working

I have installed a clean Apache2 (plus PHP & MySQL) server and enabled the mod_rewrite in the apache config. I added the .htaccess file to remove the index.php from the url as described in the CodeIgniter wiki.

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

I placed this file in the website's root.

When I try to access the url mydomain.local/index.php/welcome then I get the default page of CodeIgniter. But when I try to access the same page through mydomain.local/welcome then I get the 404 page.

How can I check if the whole rewrite rule is working? And why isn't it working?

Upvotes: 5

Views: 13662

Answers (6)

Mohd Abdul Mujib
Mohd Abdul Mujib

Reputation: 13948

If you are encountering a 500 Internal Server Error. The first place to look for is the apache error.log file. For me the error was related to the command AddOutputFilterByType.

Invalid command 'AddOutputFilterByType', perhaps misspelled or defined by a module not included in the server configuration.

For my configuration, two modules were not enabled in the apache configuration, while the respective commands were being used in the .htaccess. So once I enabled the filter_module and the deflate_module everything was back to normal.

Upvotes: 0

bramaningds
bramaningds

Reputation: 41

Default .htaccess (from ci userguide http://ellislab.com/codeigniter/user-guide/general/urls.html)

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

I have confuse above code for a moment, but i have code like this:

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

On localhost you must add "./" in front of index.php (3rd line) because in my case, ci directory not placed on root. I think "./" means it should be relative path. For additional condition ".*\.css|.*\.js|.*\.png", it means apache should not rewrite on file with extensions css, js and png.

Upvotes: 1

Sven van Zoelen
Sven van Zoelen

Reputation: 7229

I found the solution. The htaccess file wasn't allowed to run by the Apache config. So I had to set the AllowOverride flag on the directory to AllowOverride ALL.

Thanks for all the help!

Upvotes: 9

Shomz
Shomz

Reputation: 37711

Try changing the uri_protocol config settings. Also, some servers require this modification in the htaccess file:

RewriteRule ^(.*)$ /index.php/?$1 [L]

The question mark after that last slash. Try that.

Also make sure your mod_rewrite is on.

Upvotes: 0

Quetzy Garcia
Quetzy Garcia

Reputation: 1840

Assuming that your configuration (/application/config/config.php) has the index_page disabled:

$config['index_page'] = '';

And your Apache DocumentRoot is: /srv/www/

Create an .htaccess file at the same level as your /application/ directory, with the following content:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

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

You should notice that RewriteBase points to the DocumentRoot (/). If the index.php is in another directory, your should change RewriteBase accordingly.

For a directory structure like:

/srv/www/application/
/srv/www/system/
/srv/www/index.php
/srv/www/.htaccess

Your RewriteBase should be: /

For a directory structure like:

/srv/www/codeigniter/application/
/srv/www/codeigniter/system/
/srv/www/codeigniter/index.php
/srv/www/codeigniter/.htaccess

Your RewriteBase should be: /codeigniter/

And so on, you get the picture.

Upvotes: 9

Cubed Eye
Cubed Eye

Reputation: 5631

you need to remove the index.php from the index page in the config file

/application/config/config.php

$config['index_page'] = '';

Try this .htaccess stuff, it's what I'm using:

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

You may also need to enable mod_rewite in you apache config.

Upvotes: 0

Related Questions