homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

.htaccess for 301 redirect
A little strange this time.

10+ Year Member

Msg#: 3284493 posted 4:25 am on Mar 17, 2007 (gmt 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]



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

Msg#: 3284493 posted 5:38 am on Mar 17, 2007 (gmt 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].



10+ Year Member

Msg#: 3284493 posted 6:08 am on Mar 17, 2007 (gmt 0)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved