I have a CI application that uses .htaccess for URL routing. My basic setup is as follow:
RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
These rules are pretty standard for CI apps. They rewrite all URLs (except for those in the exception list) to the index.php front controller. The lines above also hide index.php, as it would normally appear as part of every URL.
So far, so good. Everything works just fine. Now, for the sake of SEO I would like to force all traffic to www. So I extended the rules as follow:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
rewritecond %{http_host} ^jungledragon.com [nc]
rewriterule ^(.*)$ http://www.jungledragon.com/$1 [r=301,nc]
These last two lines rewrite http://jungledragon.com/anything URLs to http://www.jungledragon.com/anything URLs. This kind of works, but it brings back the index.php part back: http://jungledragon.com/anything becomes http://www.jungledragon.com/index.php/anything.
How exactly do I combine these rules so that they do not interfere with each other? I tried doing the WWW rewrite before the CI rules. That shows an Apache 301 page with an error, rather than doing the actual redirect.
Additionally, I would like to also include rules to get rid of trailing slashes, but for now let’s keep the question simple. Note that I did find useful post here and elsewhere yet for some reason I still can’t find the correct exact syntax for my situation.
Edit: Thanks for the help. This works:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^jungledragon.com [nc]
rewriterule ^(.*)$ http://www.jungledragon.com/$1 [r=301,nc,L]
RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
mod_rewriteprocesses rules in a linear fashion. Rules at the top of the file are processed first.The
[nc]and[L]at the end of the rules are the options for how to process rules.You need to put your
www redirectrules above your CI rules so it will first add the www, THEN apply the CI rules to the newly re-written url. **And also use either theCorNflag with yourwww redirectrule so it will parse the next rule.http://mysite.com/blah==becomes==>http://www.mysite.com/blah==becomes==>http://www.mysite.com/index.php/blah(Executed, not redirected)What’s happening currently is:
http://mysite.com/blah==becomes==>http://mysite.com/index.php/blah(STOP)Browser goes to
http://mysite.com/index.php/blahand a second re-write pass is done since your exceptions stop /index.php urls from being processedhttp://mysite.com/index.php/blah==becomes==>http://www.mysite.com/index.php/blah(Redirected)As Suggested, here is a link to mod_rewrite’s documentation if you want to look further.
@LazyOne: Brainfart, sorry.
Here’s an excerpt from the docs outlining the flags you’ll probably need: