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?
You should place your . htaccess file at your root directory not Inside the application folder. Save this answer.
There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.
htaccess file in our project's root directory or CodeIgniter directory. Creating a . htaccess file will allow us to modify our rewrite rules without accessing server configuration files.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With