| .htaccess - how to exclude homepage from being redirected?
|
TravelSite

msg:4072027 | 5:08 pm on Feb 1, 2010 (gmt 0) | Hi, I've written some .htaccess code which is designed to redirect requests to just one file/script. There are a few exemptions to this - e.g. I don't wan't the homepage or the about us page to be redirected. The code below partially works - most things are redirected, while certain pages including the /contact-us/ avoid being redirected However I'm having a major problem with the homepage: www.foobar.com/index.php doesn't get redirected (which is what I want) - but www.foobar.com/ does get redirected. I tried adding in "||" and "|/|" to the last RewriteCond below to cover this - but when I do this nothing at all gets redirected. Is there a way around this? RewriteEngine on Options +FollowSymlinks RewriteBase / RewriteCond %{HTTP_HOST} !^www\.* RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{REQUEST_URI} !^.*.(jpg|jpeg|gif|js|css|class|png)$ [NC] RewriteCond %{REQUEST_URI} !(/category/index.php|/index.php|/contact-us/index.php|/contact-us/) [NC] RewriteRule ^.*$ /category/index.php
|
TravelSite

msg:4072043 | 5:40 pm on Feb 1, 2010 (gmt 0) | Sorry - I just realised that I'd missed out the '$' end (which makes it work - doh!) RewriteCond %{REQUEST_URI} !^(/category/index.php|/index.php|/|/contact-us/index.php|/contact-us/)$ [NC]
|
jdMorgan

msg:4072051 | 5:47 pm on Feb 1, 2010 (gmt 0) | Making the redirect rule's pattern require at least one character will fix the "/" home page rewrite problem. Fixing that, along with several errors (two serious), pattern-anchoring problems, and various redundancies and inefficiencies, yields:
Options +FollowSymlinks RewriteEngine on # RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] # RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|js|css|class|png)$ [NC] RewriteCond %{REQUEST_URI} !^/(category/index\.php$|index\.php$|contact-us/) [NC] RewriteRule ^.+$ /category/index.php [L]
Note that you should add a rule to canonicalize those "/index.php" requests by redirecting them to "www.example.com/", and should always link only to "/" within your own site. Adding this rule as the very first in the list above will take care of the code end, redirecting requests for "/index.php" in any and all directories to "/" in that same directory, but fix your on-site links first before deploying it:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]*/)*index\.php(\?[^\ ]*)?\ HTTP/ RewriteRule ^(([^/]*/)*)index\.php$ http://www.example.com/$1 [R=301,L]
Because of the [NC] flag you included on your script-rewrite exclusion RewriteConds, I assume that you receive mis-cased requests for these resources. If that is the case, then those mis-cased requests should be redirected to correctly-cased URLs to avoid duplicate-content issues. Having done so, or if you do not get a lot of mis-cased requests, remove the [NC] flags. Jim
|
TravelSite

msg:4076191 | 9:08 am on Feb 8, 2010 (gmt 0) | Thanks Jim - that's great. I want the index.php still to show (for some admin pages I use) - but have added some lines to put "/" at the end of directories - thanks for the suggestion :)
|
TravelSite

msg:4076197 | 9:29 am on Feb 8, 2010 (gmt 0) | I removed the [NC] flag from the two lines - but it still seems to allow urls in the wrong case to work. Any suggestions? Thanks
|
jdMorgan

msg:4076944 | 2:03 pm on Feb 9, 2010 (gmt 0) | Not sure how that's possible, unless you failed to delete your browser cache before testing the new code... Requests for those mis-cased URLs will no longer be excluded from the second rule, and will therefore get rewritten to "category/index.php". Jim
|
|
|