Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite infinte loop problem

multiple redirections cause infinite loop

         

drmoz

2:30 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



Hello,

Here is what I want to do:

1. I only have ASP pages on my site but I now want people to type anypage.html and be "silently" see the anypage.asp page.

2. I also want people who go to anypage.asp to be redirected (301) to anypage.html.

I can manage to do ONE or the OTHER but if I do both I run into an infinite loop problem.

Here is the code I use:

RewriteRule ^(.*).html$ $1.asp [L]
RewriteRule ^(.*).asp$ $1.html [R=301,L]

PLEASE HELP

Thanks

jdMorgan

3:07 am on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use THE_REQUEST to break the loop:

# Redirect only direct client HTTP requests for .asp to .html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^.]+\.asp\ HTTP/
RewriteRule ^([^.]+)\.asp$ http://www.example.com/$1.html [R=301,L]
#
# Internally rewrite .html requests to .asp pages
RewriteRule ^([^.]+)\.html$ $1.asp [L]

Jim

phranque

6:09 am on Nov 14, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], nico!

carguy84

6:19 am on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you also just switch the order in which they appear?

RewriteRule ^(.*).asp$ $1.html [R=301,L]
RewriteRule ^(.*).html$ $1.asp [L]

I've only used ISAPI Rewrite, not familiar with mod-rewrite

jdMorgan

6:43 am on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, because the external redirect will result in a new HTTP request, so you get a loop either way.

Jim

drmoz

11:16 am on Nov 14, 2007 (gmt 0)

10+ Year Member



Hello,

I tried the code below.

# Redirect only direct client HTTP requests for .asp to .html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^.]+\.asp\ HTTP/
RewriteRule ^([^.]+)\.asp$ http://www.example.com/$1.html [R=301,L]
#
# Internally rewrite .html requests to .asp pages
RewriteRule ^([^.]+)\.html$ $1.asp [L]

But it got me into an infinite loop again.

Thanks

jdMorgan

3:25 pm on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Flush your browsers cache. If that doesn't fix it, then we need to see some actual requested URL examples (with "example.com" as the domain, please).

Jim

drmoz

3:47 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



Hi,

I'll give you a precise example of what I need to do.

2 things in fact:

1. When people type http://www.example.com/mypage.html
They're "silently" (they keep seeing the mypage.html in the address bar) redirected to http://www.example.com/mypage.asp

AND

2.As I don't want the search engines to see duplicate content on my site (mypage.html and mypage.asp), I also need to permanently (301) redirect people from http://www.example.com/mypage.asp to
http://www.example.com/mypage.html

When I try to do that, I get a loop problem. The same happened with the code you gave me.

I also cleared my cache but the problem remained.

Thanks

nico

[edited by: jdMorgan at 3:56 pm (utc) on Nov. 14, 2007]
[edit reason] example.com [/edit]

jdMorgan

4:01 pm on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is in fact what the code I posted does. It's code that I use on many servers.

So, either you didn't flush your browser cache completely or, like another person posting this morning, you have a caching proxy in your network path.

Another possibility is that there is another rule or scripted routine that interfering with this simple two-rule code snippet. A good way to spot this is by using the "Live HTTP Headers" add-on for Firefox and Mozilla browsers; Request the .asp URL, and watch the sequence of redirects to see if there is a redirect that is *not* produced by the mod_rewrite code, or to see if the URLs are different from what you expect. For example, if the .asp URLs have query strings attached, then the code above won't handle them, and will need to be modified.

Jim

[edited by: jdMorgan at 4:02 pm (utc) on Nov. 14, 2007]

drmoz

4:12 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



I've done what you said.

I've installed Live HTTP headers. I've also made sure the your code is the only thing on the script.

When I go to

http://www.example.com/mypage.asp

It redirects me to

http://www.example.com/mypage.html

Then it keeps redirecting me to that same page over and over again until an error message is thrown.

Thanks

nico

[edited by: jdMorgan at 2:19 pm (utc) on Nov. 15, 2007]
[edit reason] example.com [/edit]

jdMorgan

11:04 pm on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the code being used is in exactly the form I showed above, with no changes except for the domain name, then this redirection loop is not happening because of that code, and you need to be looking for another 'agent' that is generating a redirect or internal rewrite, countermanding or interfering with one or both of those rewriterules. Another set of rewriterules, perhaps, or a routine in one of your scripts -- even an inconsistent domain canonicalization rule might do this.

The external redirect above can only be invoked if the client sends a request for a .asp page, while the internal rewrite is only invoked for .html page requests. So, it should be clear from examining the code itself that neither rule can redirect .html to .html, and as soon as the client requests a non-asp page, the redirect rule is disabled.

That's why I asked about flushing your cache twice; The code is very simple and works fine on dozens of my servers. Many others here have used the same code, bringing the installed base to dozens if not hundreds or even thousands more.

Jim

phranque

3:31 am on Nov 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



if you have access to the httpd.conf file you could try setting a higher RewriteLogLevel [httpd.apache.org].

drmoz

4:29 am on Nov 15, 2007 (gmt 0)

10+ Year Member



Would the code below (for mod rewrite) be the same for ISAPI rewrite?

# Redirect only direct client HTTP requests for .asp to .html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^.]+\.asp\ HTTP/
RewriteRule ^([^.]+)\.asp$ http://www.example.com/$1.html [R=301,L]
#
# Internally rewrite .html requests to .asp pages
RewriteRule ^([^.]+)\.html$ $1.asp [L]

thanks

jdMorgan

2:12 pm on Nov 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, the syntax is quite different for ISAPI Rewrite -- although the basic idea is the same.

Jim