Forum Moderators: phranque

Message Too Old, No Replies

Order of redirects?

         

Handsome Prince

12:28 am on Feb 1, 2011 (gmt 0)

10+ Year Member



Hi Jim,

I read through the different threads concerning 301 redirects. I would like to use the staple examples I saw posted here:

Apache redirect /index.html to /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]

Apache redirect example.com to www.example.com

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

Couple of quick newbie questions:

1. I believe it was suggested to put the index redirect first, is that correct? And if so, is that because it is the more specific rule or is it for another reason (just curious)?

2. In the {THE_REQUEST} sample code (to redirect "/index.html" to "/"), do I need to insert a [NC] at the end to cover both "index.html" and "Index.html"?

3. I noticed in some other posts discussing redirecting example.com to www.example.com that instead of redirecting example.com to www.example.com, the code seemed to redirect everything that was not www.example.com to www.example.com, as in:

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

While "stranger" to look at, I guess this is preferred in terms of optimization?

4. I have an old fashioned location.replace redirect in place to redirect "Index.html" to "/" (i.e. index.html). I guess there's no harm/conflict in keeping this old fashioned redirect file (Index.html) on the server?

Thanks a bunch,

HP

g1smd

12:44 am on Feb 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, index rule first because it is "more specific". Consider if the non-www rule were first and there was a request for example.com/index.html. There would be an unwanted double redirect in that case: non-www -> www followed by index -> / next.

Yes, I believe that the [NC] flag would cover both cases, and many others such as inDex.hTml too.

Yes, if there is only one site involved in the actual serving of content, then using a rule to redirect everything that is not "exactly" www.example.com is much simpler code. The additional ( )? code stops HTTP/1.0 (with blank host name value) requests triggering an infinite loop.

If requests for a URL are handled in .htaccess and redirected to a different URL, then content within any file inside the server that would have previously been served for that URL request, will no longer be seen by the browser.

Handsome Prince

1:08 am on Feb 1, 2011 (gmt 0)

10+ Year Member



Thank you! With regard to Q3, could you clarify what you mean by
Yes, if there is only one site involved in the actual serving of content
Doesn't a given .htaccess file in a folder on example.com affect only URL calls to that particular website?

Thank again!

g1smd

8:07 am on Feb 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sometimes people have a folder on their server with a script that serves content for multiple domains. In that case, a "redirect all" directive makes all but one domain fail. It's a rare setup, but mentioned "just in case".