Forum Moderators: phranque
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
# 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]
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
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]
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]
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]
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
# 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