Forum Moderators: phranque

Message Too Old, No Replies

Another way to Redirect and Rewrite

         

CWebguy

8:16 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



Ok, I found this on another forum, but it was pretty useful to me, thought I would maybe share here.

I haven't tested this, but, what happens in a redirect to a rewrite is the old URL gets redirected to the new, which a lot of times gets rewritten back to the old (dyanmic) which gets redirected again, creating an endless loop.

Of course, one solution is {THE_REQUEST} as mentioned here before, another which should create the same results, is to add an extra parameter or string tacked onto the end of the rewritten URL. (Since the client will never see the rewritten URL, it can be anything), and as long as you are doing an exact match for the redirect, it won't trigger/match the redirect again.

Just thought this might be useful.

Thanks for all the help here and keep up the good work!

CWebguy

[edited by: CWebguy at 8:18 pm (utc) on Mar. 12, 2009]

g1smd

8:23 pm on Mar 12, 2009 (gmt 0)

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



There is one more thing that I often see people omit to think about in "dynamic to static" redirects; and that is the parameter order in the request.

They have a redirect for

/category.php?cat=1&sub_cat=2
in place, but not one for
/category.php?sub_cat=2&cat=1
and so requests using that URL format fail to be redirected.

What about trailing ampersand, and/or leading or trailing junk too? They are all valid requests. If they are not redirected then the request WILL be passed to the script for processing - and if it returns content and 200 OK status, then that's yet another duplicate URL.

jdMorgan

1:24 am on Mar 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The best thing about the method using %{THE_REQUEST} is that it is self-documenting. Other methods are to set a "user" variable on the rewrite using the [E=var:val] flag on RewriteRule and check for that variable on the redirect, or to use the "REDIRECT" variables, such as %{REDIRECT_REQUEST_URI}, etc. to detect previously-rewritten values.

There are often dozens of correct ways to do things (along with thousands of incorrect ways). Among the correct ways, the one(s) you choose should be based on efficiency and simplicity. Beyond that, it's simply a matter of personal 'style.'

Jim

CWebguy

9:19 pm on Mar 13, 2009 (gmt 0)

10+ Year Member



Good point G1, thanks.