Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess for 301 redirect

A little strange this time.

4:25 am on Mar 17, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Jan 19, 2003
votes: 0

What the differnt between the following two 301 redirect methods?

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

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

I have this question, becuase I find occasionally that method 2 will always return 301 header when I use a tool to get header of a www.example.com. Maybe the tool has some bug in it but it's necessary for me to find the reason of this issue.

[edited by: iProgram at 4:26 am (utc) on Mar. 17, 2007]

5:38 am on Mar 17, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0

Method 1 says "Redirect if example.com is requested."

Method 2 says "Redirect if anything other than www.example.com is requested."

Your code in method 2 has a "hole" in it. Specifically, if used on a host that is HTTP/1.0 accessible, it can cause an HTTP/1.0 client to go into a loop, because HTTP/1.0 does not support the client sending the "Host:" header that Apache uses as %{HTTP_HOST}. Therefore, %{HTTP_HOST} will always be blank for HTTP/1.0 requests, and therefore, the rule will always issue a redirect. It can be easily fixed:

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

This simply adds a check to ensure that HTTP_HOST is non-blank -- that it contains at least one character, to prevent a redirect if it is blank. You may also see this written in a longer equivalent form as "!^$" that is, "NOT blank."

Note that this does not apply to name-based virtual servers; Name-based virtual servers also depend on the HTTP/1.1 "Host:" header to "select" a particular named host; Therefore an HTTP/1.0 request to a name-based virtual server will always return the default server, and not the one expected.

Note that some clients "advertise" as HTTP/1.0, but are actually "enhanced" HTTP/1.0, in that they *do* send a Host header. Many robots, including googlebot, advertise as HTTP/1.0, but send a "Host:" header. It is possible your 'tool' is sending true HTTP/1.0 requests with no "Host:" header.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].


6:08 am on Mar 17, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Jan 19, 2003
votes: 0

So I will change all my .htaccess file to the method 1 now...Thanks a ton!

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members