Welcome to WebmasterWorld Guest from 54.236.246.85

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

301 redirect displayed even for valid domain

Webbug shows 301 redirect even when it shouldn't....

     
6:12 pm on Jul 14, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:Oct 7, 2003
posts:539
votes: 0


I have an .htaccess file for my domain to redirect all requests for [domain.com...] to [domain.com...] -- but when using WebBug to display the headers, it shows a 301 redirect even when it shouldn't. My .htaccess file contains:

Options FollowSymLinks ExecCGI Includes
AddType text/html .html
AddHandler server-parsed .html
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteRule ^(.*) [domain.com...] [L,R=301,QSA]

When going to [domain.com...] there should be no 301 redirect, but there is according to WebBug:

HTTP/1.1 301 Moved Permanently
Date: Wed, 14 Jul 2004 18:11:42 GMT
Server: Apache
Location: [domain.com...]
Content-Length: 235
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.domain.com/">here</a>.</p>
</body></html>

Does anyone have any ideas on why this is happening, or how to fix it?

7:02 pm on July 14, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:May 4, 2004
posts:525
votes: 0


This could happen if a request is made according to the http specification prior to 1.1, where the host header is not required in each request header, so if your rewriting runs into a request like this it matches (because an empty string is not equivalent to www.domain.com) so it redirects the browser. A workaround is to exclude requests with empty host header from the rules, something like this:

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

Note the additional dollar signs at the end of the patterns. Should not make any difference, but put it there to be on the safe side.
7:08 pm on July 14, 2004 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


Assuming that you have a space before the "!", the code you have should work.
Is it possible that you might have some code in httpd.conf (server configuration) that is interfering, or perhaps you're using a "pointed domain"? Or maybe your server is not passing the HTTP_HOST variable for some reason; if it is blank, then you'd always get a redirect. Otherwise, I can't think of any reason why the code wouldn't work in a stand-alone environment.

As a work-around, you could try changing the code from "redirect unless" to "redirect if" like this:


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

Also, you should not need the query string append flag, unless you wish to add a new parameter to the existing query string. Otherwise, the original query should be passed by default.

Jim

7:21 pm on July 14, 2004 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


OT to the main question, but I never end-anchor HTTP_HOST patterns, unless it is absolutely required. In that case, I use a patten like:

^www\.domain\.com(:[0-9]{1,5})?$

Mozilla user-agents can and will append a port number to the hostname, and that can cause unexpected results with end-anchored patterns that don't comprehend the port number.

Jim

9:30 pm on July 14, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:Oct 7, 2003
posts:539
votes: 0


Thanks everyone for the tips. I did discover that WebBug was send the info using HTTP 1.0 instead of 1.1, changing it to 1.1 fixed the problem. But I seemed to recall that some search engines use(d) the 1.0 protocol. So to be on the safe side I used the code gergoe provided (though without the end anchor $):

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^(.*)$ [domain.com...] [R=301,L]

WebBug displays the page when accessing [domain.com...] under HTTP 0.9 and 1.0, and the 301 redirect under HTTP 1.1. WebBug displays the page under all protocols when accessing [domain.com...] as it should. So the above code seems to do the trick.

Many thanks again to all who responded.

11:34 pm on July 14, 2004 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


Interestingly, most search engines "advertise" HTTP/1.0, but actually provide the Hostname request header as required by HTTP/1.1.

If they did not provide the hostname header, then they would be unable to spider the vast number of sites on shared-IP name-based virtual hosts.

Nevertheless, it *is* a very good idea to make sure the hostname is present before acting upon it.

Jim