Forum Moderators: phranque

Message Too Old, No Replies

301 redirect issue

recieving improper headers

         

phish

4:03 am on Jan 31, 2006 (gmt 0)

10+ Year Member



I just did a redirect from index.htm > www.domain.com
like so:
redirect 301 /index.htm [domain.com...]

problem is when i check www.domain.com/index.htm i get the proper 301 redirect with location showing www.domain.com, but when I check www.domain.com/ it also shows a 301 redirect instead of the proper 200 ok.
any suggestions?

thanks
-phish

apache version is 1.3.33 if it matters

jdMorgan

4:52 am on Jan 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is index.htm still your index page thats shows when you request "/"?

If so, it's likely you've put your server in a loop... You may find that the page load time is slower (flush your cache to test this).

If index.htm is still declared as your index page (See Apache DirectoryIndex directive), then you need to use a 'tricky' bit of code to prevent problems. It requires the use of mod_rewrite and checking a particular server variable to prevent this looping.


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

The trick here is that because of the RewriteCond directive, the redirect will occur *only* if the request for "/index.htm" came from the client (e.g. browser or robot), and not as the result of the internal 'redirect' that the server does when "/" is requested so that it can serve the contents of index.htm. This prevents a URL rewrite loop.

Note that THE_REQUEST is the entire client request header. For example:

GET /index.htm HTTP/1.1

For background information on mod_rewrite and regular expressions, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

[edit] Corrected formatting problem. [/edit]

[edited by: jdMorgan at 2:32 pm (utc) on Jan. 31, 2006]

phish

5:23 am on Jan 31, 2006 (gmt 0)

10+ Year Member



hey jim...
thanks alot..that should work.

<props>by the way i'd also like to say how much I personally appreciate the time, and dedication, you and the other mods put into this forum helping others out. It is truly appreciated, and I want you to know your hard work and effort does not go unnoticed</props>

phish

6:09 am on Jan 31, 2006 (gmt 0)

10+ Year Member



works fine...

phish

8:40 am on Jan 31, 2006 (gmt 0)

10+ Year Member



but now....my non www to www redirect isn't working.
=(

heres how my htaccess looks

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.htm\ HTTP/
RewriteRule ^index\.htm$ [example-domain.com...] [R=301,L]

RewriteCond %{HTTP_HOST}!^www\.example-domain\.com
RewriteRule (.*) [example-domain.com...] [R=301,L]

any ideas what i broke..lol

ps...there is a hyphen in my domain..does it need to be escaped?

thanks
-phish

jdMorgan

2:21 pm on Jan 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's nothing wrong with your code. The only thing I see is probably an artifact of posting here, and that is that a space is required between "}" and "!" in the RewriteCond. The forum software removes spaces preceding exclamation marks, unless you type two spaces or precede the "!" with a tag pair such as [ b ][ /b ].

Be sure to flush your browser cache before testing after making *any* change to your configuration files.

Hyphens don't need to be escaped.

Jim

TristanToxic

3:34 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



I seem to be having a problem with this

this is my .htaccess:

ErrorDocument 400 /400.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^ht*p://www.example.com.*$ [NC]
RewriteCond %{HTTP_REFERER}!^ht*p://www.example.net.*$ [NC]
RewriteCond %{HTTP_REFERER}!^ht*p://example.com.*$ [NC]
RewriteCond %{HTTP_REFERER}!^ht*p://example.net.*$ [NC]
RewriteCond %{HTTP_REFERER}!^ht*p://xx.xx.xx.xx.*$ [NC]
RewriteCond %{HTTP_REFERER}!^ht*p://.*\.google\..*$ [NC]
RewriteCond %{HTTP_USER_AGENT}!^Googlebot.*$
RewriteCond %{HTTP_USER_AGENT}!^Mediapartners.*$
RewriteCond %{HTTP_USER_AGENT}!^Scooter.*$
RewriteRule (\.gif)¦(\.jpg)¦(\.mpg)¦(\.wmv)¦(\.avi)¦(\.mpeg) - [F]

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

the first is an anti-hotlinking part, the second to put www. in front

now if i request example.com/testa/testb/testc/ it seems to redirect to www.example.com/testb/testc/

anyone knows?

TristanToxic

3:39 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



I might add to this that this previous post was the .htaccess in my /testa/ directory (so ht*p://www.example.com/testa/.htaccess ) and that my "main" .htaccess (/.htaccess) also has the rewrite lines:

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

TristanToxic

3:41 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



stupid me, I changed the /testa/.htaccess to

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

and everything works now

thanks anyhow ;)

jdMorgan

4:32 pm on Jan 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, let's make it worthwhile, then...

This code appears to be auto-generated, and is far from efficient. You can accomplish the same thing with only six lines:


RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.(com¦net) [NC]
RewriteCond %{HTTP_REFERER} !^http://192\.168\.10\.10/
RewriteCond %{HTTP_REFERER} !^http://([^.]+\.)*google\. [NC]
RewriteCond %{HTTP_USER_AGENT} !(Googlebot¦Mediapartners-Google¦Scooter)
RewriteRule \.(gif¦jpg¦mpe?g¦wmv¦avi)$ - [F]

That should be a bit faster...

Change the broken pipe "¦" characters to solid pipe characters before use; Posting on this board modifies them.

Jim

TristanToxic

5:23 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



Thanks alot! :-)

TristanToxic

8:18 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



Now jdMorgan, I was wondering: instead of putting those non-www to www , index.html to / , and anti-hotlink rewrite rules in .htaccess, they would probably be alot faster if I placed them in httpd.conf no? Does any syntax changes?

Thanks!

jdMorgan

8:43 pm on Jan 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the case of the code above, nothing would change. The main difference between .htaccess and httpd.conf contexts is that the URLs 'seen' by RewriteRules in .htaccess are localized to the directory in which that .htaccess file resides.

For example, if you had an .htaccess file in the directory /mystuff/widgets that said:


RewriteRule ^green_widget\.html$ /widgets.php?prod=widget&color-green [L]

then the equivalent in httpd.conf would be

RewriteRule ^/mystuff/widgets/green_widget\.html$ /mystuff/widgets/widgets.php?prod=widget&color-green [L]

The other major difference is that the code in httpd.conf must be re-compiled by restarting the server before it takes effect. However, since this code is compiled in httpd.conf versus interpreted in .htaccess, it does execute faster in httpd.conf.

Jim

TristanToxic

11:34 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



I modified my things a bit so non-www goes to www. , and that www.example.com/<anything>/index.html goes to www.example.com/<anything>/
it works, but I was wondering if my solution is safe and if there isn't a shorter/safer/quicker alternative?
(this is for httpd.conf)

----------
DocumentRoot /usr/local/apache/www/example.com/htdocs
<Directory /usr/local/apache/www/example.com>
AllowOverride All
Options +FollowSymLinks
RewriteEngine on

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.(html¦htm¦shtml)\ HTTP/
RewriteRule ^htdocs(.*)/ http://www.example.com$1/ [R=301,L]
</Directory>
----------

Thanks

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



Well, I'd reverse the two rule-sets, so that a request for http://example.com/index.shtml doesn't have to go through two external redirects. But other than that, it looks fine.

You could also use the pattern

/([^/]+/)*index\.s?html?\ HTTP/

instead of

/.*index\.(html¦htm¦shtml)\ HTTP/

to speed things up a bit.

Jim