CodeIgniter Forums

Full Version: htaccess redirects
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]brian88[/eluser]
For my codeigniter project I have the following code in my .htaccess

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

When I go to mysite.com it redirects me to www.mysite.com.... great

But when I go to mysite.com/any_page it redirects to www.mysite.com/index.php?/any_page, which is invalid and just goes to the home page

My uri_protocol= AUTO;

Any ideas?

El Forum

[eluser]CroNiX[/eluser]
Yes, your CI rules should be dead last in your htaccess, after any other redirects or other manipulation take place.

Otherwise, if you go to "yoursite.com/anypage", it hits the first rules dealing with CI. So it then passes the request to CI, which processes it. Now it comes to your force www rule and says "oops", no www, redirect the whole thing to www and start over.
Code:
RewriteEngine On
#Force WWW
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

#Handle CI
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

El Forum

[eluser]brian88[/eluser]
Thanks Cronix, your awesome.