5

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?

Sven van Zoelen
  • 6,989
  • 5
  • 37
  • 48

6 Answers6

9

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.

Quetzy Garcia
  • 1,820
  • 1
  • 21
  • 23
9

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!

Sven van Zoelen
  • 6,989
  • 5
  • 37
  • 48
1

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.

bramaningds
  • 41
  • 1
  • 5
0

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.

Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
0

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.

Cubed Eye
  • 5,581
  • 4
  • 48
  • 64
0

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.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Mod_rewrite is on and is loaded based on the phpinfo() info. But the addition is not working. I really start wondering if the whole rewrite mod is working... – Sven van Zoelen Nov 25 '11 at 13:02
  • Hm, is it rewriting ANYTHING? Do some tests, even outside CI, then you'll be able to localize the problem. – Shomz Nov 25 '11 at 14:16