Forum Moderators: phranque

Message Too Old, No Replies

Getting an infinite loop after changing dynamic urls to static

         

Tannu

7:47 am on Mar 10, 2012 (gmt 0)

10+ Year Member



Hi,

Its a simple task of changing dynamic URLs to static. The problem is that both rewriting and redirection as individual entities are working properly but when put together are resulting in a self-referencing loop.

Here's a section of code of .htaccess:


RewriteRule ^alpha-beta$ /filename.htm\?page=menu4&sub=content_m4-3 [L]

RewriteCond %{QUERY_STRING} ^page=menu4&sub=content_m4-3$
RewriteRule ^filename\.htm$ http://www.domain.com/alpha-beta? [R=301,L]



Now when I go to the first dynamic version i.e. www.domain.com/filename.htm?page=menu4&sub=content_m4-3, it redirects to static www.domain.com/alpha-beta but then it again gets redirected to itself i.e. www.domain.com/alpha-beta and thus a loop is being generated.

g1smd

7:56 am on Mar 10, 2012 (gmt 0)

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



For the RewriteCond in the redirect code, test THE_REQUEST not QUERY_STRING.

This will ensure you redirect requests only when the pointer contains a query string as a result of an external request for a URL with query string and not as a result of a prior internal rewrite.

List the rule which redirects first. Follow it with a general non-www to www redirect for all other requests. List the internal rewrite last.

Tannu

8:42 am on Mar 10, 2012 (gmt 0)

10+ Year Member



Hey g1smd,

Thanks for the quick reply but I tried it the way you asked or the way I thought you asked but now the redirection doesn't work at-all, but the rewrite rule works fine as before.
Here's the code:


Options +FollowSymlinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^page=menu4&sub=content_m4-3$
RewriteRule ^filename\.htm$ http://www.domain.com/alpha-beta? [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteRule ^alpha-beta$ /filename.htm\?page=menu4&sub=content_m4-3 [L]


Actually if this helps than I also tried on the non-dynamic version in another website i.e. a same kind of redirection/rewriting but from static to static just changing the name of the file to anything arbitrary but it also has the same issue(infinite loop).

The issue could be that when a user goes to the first dynamic URL or any URL being redirected(301) it goes to the next static URL, then that static URL is rewritten (not redirected) back to the dynamic version so as to access the correct file, but somehow for the .htaccess file in this case it again fits the criteria for the 301 redirection (the first rule being implemented).

I know its fuzzy but going nuts after it for past 21hrs or so(not in continuation) ;)

Appreciate you looking over it

lucy24

9:09 am on Mar 10, 2012 (gmt 0)

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



In brief:

Outside requests for {long messy URL} get redirected to {short pretty URL}.
All requests for {short pretty URL} get rewritten to {long messy URL}. The two long messy URLs may or may not be identical, depending on whether you're changing other stuff at the same time.

So the structure is

#1
RewriteCond %{THE_REQUEST} {long messy URL}
RewriteRule {long messy URL} {short pretty URL} [R=301,L]

#2
RewriteRule {short pretty URL} {long messy URL} [L]

That's the bare bones. The Redirect will include the full protocol and domain; you don't need to test for it separately, because everything is getting redirected anyway. Later on you'll have a mop-up rule that grabs any requests for the wrong domain name-- such as an unwanted www-- and redirects to the desired form. But that's completely separate from the rewrite-and-redirect two-step that you're doing here.

Put all your RewriteRules in order of severity: first [F] then [G] then [R=301] and finally the plain Rewrites without Redirect.

Most important but often overlooked: Make sure every single one of your site's internal links points to {short pretty URL}. The redirect in your htaccess is only for people following outdated links or bookmarks from other places.

Tannu

9:42 am on Mar 10, 2012 (gmt 0)

10+ Year Member



Hey,

I tried it your way but the redirection ain't working. Can you give me the code for it as I am not quite familiar with the usage of The_Request or as I mentioned, it is not only with the dynamic version but also with the static version when I try to rename it to something else through the combination of rewrite/redirect, I get the same loop.

It is something to do with the code i guess. Some small thing I am missing. Please refer to the code above that I am using and see if you can come up with something. Have tried most of the versions but not the recipe that works, apparently.

lucy24

8:09 pm on Mar 10, 2012 (gmt 0)

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



RewriteRule ^alpha-beta$ /filename.htm\?page=menu4&sub=content_m4-3[L]

RewriteCond %{QUERY_STRING} ^page=menu4&sub=content_m4-3$
RewriteRule ^filename\.htm$ [domain.com...] [R=301,L]

Now when I go to the first dynamic version i.e. www.domain.com/filename.htm?page=menu4&sub=content_m4-3, it redirects to static www.domain.com/alpha-beta but then it again gets redirected to itself i.e. www.domain.com/alpha-beta and thus a loop is being generated.

As g1 said: The crucial condition is
%{THE_REQUEST}
This means: "what the user originally asked for". Or, in plain English, "a new line in your logs". The result of a Redirect is a fresh Request. The result of a Rewrite is not.

The original two rules are backward. First comes the Redirect with its essential Condition looking at THE_REQUEST. Then comes the Rewrite. Once something has been rewritten (not redirected), mod_rewrite should never see it again.

You do not need to say anything at all about the Query String in your condition. It's implicit in the Request.

Also: what is "alpha-beta" in real life? Does it actually exist, and if so, is it a file or a directory? Better step back a little and say in English:

#1 What your current addresses look(ed) like, before you decided to rewriting and/or redirect.
#2 What you want users to see in their address bar.
#3 Where the content really lives.

Give specific examples, but use example.com as the domain name. (Preview before clicking Submit and you will understand why.)

g1smd

10:00 pm on Mar 10, 2012 (gmt 0)

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



This is the problem:

RewriteCond %{THE_REQUEST} ^page=menu4&sub=content_m4-3$



THE REQUEST will be something like:

GET /filename.html?page=menu4&sub=content_m4-3 HTTP/1.1



This is the literal GET request that your browser sends.

Your RegEx pattern needs to match with that.

Tannu

1:37 pm on Mar 12, 2012 (gmt 0)

10+ Year Member



Thanks guys for helping me out. Finally figured it out. Here's what worked for me:


RewriteCond %{The_Request} /filename.html\?page=menu4&sub=content_m4-3\ HTTP/
RewriteRule ^(.*)$ http://www.domain.com/alpha-beta? [R=301,L]

RewriteRule ^alpha-beta$ /filename.html?page=menu4&sub=content_m4-3 [L]


Appreciate your help.

g1smd

9:51 pm on Mar 12, 2012 (gmt 0)

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



Replace
(.*)
with the actual path part of the request otherwise the
RewriteCond
also has to be evaluated for every image request and every request for css or Javascript files.

The first part of the
RewriteCond
pattern should be
^[A-Z]{3,9}\ /
to match with the literal
GET
part. Escape the literal period in this pattern.