Forum Moderators: phranque

Message Too Old, No Replies

Redirect and Rewrite Infinite loop

         

wakasenshi

5:43 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



I am new to .htaccess files so I wanted to get a static version working before I try to tackle a dynamic one.

If someone requests mydomain.com/ugly.html
I want the URL to be rewritten to mydomain.com/pretty.html
But I want it to actually show the ugly.html file

So whether they type in mydomain.com/ugly.html
or they type in mydomain.com/pretty.html
they get the ugly.html
but the URL shows mydomain.com/pretty.html

so far I have got:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^ugly\.html$ pretty.html [R=301,L]
RewriteRule ^pretty\.html$ ugly.html [L]


This results in an infinite loop.

I don't know how to fix this.

g1smd

6:22 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The redirect needs to additionally specify the domain name in the redirect target.

The redirect also needs a preceding RewriteCond checking THE_REQUEST so that only external direct client requests are redirected.

Otherwise you've almost nailed the code you need. This redirect/rewrite problem is dicussed many times each month, so there's a lot of prior example code here.

Also, do
# comment
your code so you know what each chunk does.

wakasenshi

8:17 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



Thanks for replying so quickly

The redirect needs to additionally specify the domain name in the redirect target.


Doesn't this make it so I don't need to specify the domain name?

RewriteBase /


The redirect also needs a preceding RewriteCond checking THE_REQUEST so that only external direct client requests are redirected.


I figured I needed to have a RewriteCond statement but I have searched the web for days to understand how and when to use them but I have yet to find a tutorial that explains it in terms I can understand.

It may be a lot to ask but could you explain how to use this?

RewriteCond %{THE_REQUEST}


Thanks for your patience.

jdMorgan

8:51 pm on Jun 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteBase has nothing to do with domains, it has to do with server-internal filepaths -- see the mod_rewrite docs.

Options +FollowSymLinks
#
RewriteEngine On
RewriteBase /
#
# Externally redirect direct client request for 'ugly.html' filepath to /pretty.html URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /ugly\.html\ HTTP/
RewriteRule ^ugly\.html$ http://www.example.com/pretty.html [R=301,L]
#
# Internally rewrite requests for /pretty.html URL-path to /ugly.html filepath
RewriteRule ^pretty\.html$ /ugly.html [L]

THE_REQUEST is the original client request, exactly as logged in your raw server access log. Its use here prevents recursion after the second rule has already rewritten the requested /pretty.html URL-path to the /ugly.html filepath, and thus stops the looping.

Perhaps what isn't obvious is that mod_rewrite in the .htaccess context is recursive: mod_rewrite processing is re-started each time a rule matches and is invoked, and this continues until a pass is made through all rules without any further matches and invocations.

Jim

[edited by: jdMorgan at 3:13 pm (utc) on Jun 28, 2010]

wakasenshi

2:45 pm on Jun 28, 2010 (gmt 0)

10+ Year Member



Thank you for your help