Forum Moderators: phranque

Message Too Old, No Replies

Help with a rewrite problem

Trying to rewrite without creating an endless loop

         

nadkarniarjun

12:22 am on Dec 23, 2005 (gmt 0)

10+ Year Member



Hi all! My problem is as follws:

My homepage is located at "/document/home". However, I want to be able to rewrite it as "/", i.e. root. Further, I want all requests for "/document/home" to 301 redirect to "/". The snippet from my .htaccess file is as follows, but I get stuck in an endless loop:
----------------------------------

RewriteRule ^document/home/$ / [R=301]
RewriteRule ^document/home/$ / [R=301]
RewriteRule ^$ /document/home/ [L]

jdMorgan

12:57 am on Dec 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, there's a trick to it:

# Externally redirect only original client requests for /document/home with optional trailing slash
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /document/home/?\ /HTTP/
RewriteRule ^document/home/?$ http://www.example.com/ [R=301,L]
# Internally rewrite requests for document_root "/" to /document/home/
RewriteRule ^$ /document/home/ [L]

THE_REQUEST is the original client (browser) request, for example:

GET /document/home HTTP/1.1

This variable is not affected by any internal rewrites, and so can be used to prevent the loop.

Jim

nadkarniarjun

1:13 am on Dec 23, 2005 (gmt 0)

10+ Year Member



Hi jd Morgan:

That seems very logical, but unfortunately it doesn't work.

I put the lines in my .htaccess file as follows:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /document/home/?\ HTTP/
RewriteRule ^document/home/?$ http://www.example.com/ [R=301,L]
RewriteRule ^$ /document/home/ [L]

It correctly rewrites / to /document/home. But doesn't redirect /document/home/ to /

Thanks for your help!

[edited by: jdMorgan at 1:44 am (utc) on Dec. 23, 2005]
[edit reason] Corrected per msg#4 [/edit]

nadkarniarjun

1:38 am on Dec 23, 2005 (gmt 0)

10+ Year Member



Actually, I did some more research, and I think it's because of /HTTP/. I removed the first slash, so it's just HTTP/ now.

Works Perfectly!

Thanks jdMorgan!

jdMorgan

1:45 am on Dec 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, sorry for the typo. Corrected above for later readers of this thread.

Jim