Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule Problem

RewriteRule not functioning correctly

         

Rick2277

2:14 pm on Apr 23, 2011 (gmt 0)

10+ Year Member



I am trying to run an link tracker/cloaker on my Apache web server hosted at Host Gator.

When I add the code provided by Adtrackz (the program author)
all of my web sites get errors hosted on this domain and sub-domains.

Here is the code added to the .htaccess at the root level:

RewriteEngine On
RewriteRule ^go/([a-zA-Z0-9]+)
http://www.My Domain/go/go.php?c=$1


Do you see any issues with this code?

Thanks ;-)

g1smd

2:31 pm on Apr 23, 2011 (gmt 0)

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



I see an infinite redirect loop.

The unanchored ^go/([a-zA-Z0-9]+) pattern will re-match the go/go part of the URL request after the redirect and will redirect again.

Additionally, the code produces a 302 redirect and the [L] flag is missing.

Use example.com in the forum to stop URL auto-linking.

Rick2277

12:03 am on Apr 25, 2011 (gmt 0)

10+ Year Member



Thanks for seeing the errors but I am unsure exactly how to anchor the go/go part of the expression. Adding the [L] tag is simple enough. How would you solve this infinite loop?

Thanks again ;-)

lucy24

3:42 am on Apr 25, 2011 (gmt 0)

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



Sometimes it helps to start by spelling out in English exactly what you want to have happen, and under what circumstances you want it to happen. That is, if you can't pinpoint what you're doing wrong, backtrack to asking how you would do what you're trying to do.

What error are you getting? I have to assume your server does something eventually about infinite loops, or you'd have had a meltdown by now.

g1smd

7:41 am on Apr 25, 2011 (gmt 0)

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



Try adding $ to the end of the pattern.

Now it will match only "extensionless" URL requests that contain only a-z and 0-9.

jdMorgan

5:08 pm on Apr 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This rule should almost certainly be coded as an internal rewrite, and not a client redirect. Accurately commenting the code would be useful both to us and to yourself in the future...

RewriteEngine On
#
# Internally rewrite requests for URL-path "/go/<something>" to script filepath plus query-string "/go/go.php?c=<something>"
RewriteRule ^go/([a-zA-Z0-9]+)$ /go/go.php?c=$1 [L]

Jim