CodeIgniter Forums
htaccess redirects - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: htaccess redirects (/thread-53236.html)



htaccess redirects - El Forum - 07-16-2012

[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?


htaccess redirects - El Forum - 07-16-2012

[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]



htaccess redirects - El Forum - 07-16-2012

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