Rewriterule for CodeIgniter not working

2019-04-08 11:51发布

问题:

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?

回答1:

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.



回答2:

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!



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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.