1

Im just learning mod_rewrite and regex stuff, and what I'm trying to do is pass variables of any name, with any number of variables and values, into a script and have them forwarded to a different script.

here is what I have so far:

RewriteEngine on
RewriteRule ^script\$(.*[\])? anotherscript?ip=%{REMOTE_ADDR}&$1 [L] 

That all seems to work except that one of the parameters I'm passing is a URL and the // after http:// always gets stripped down to one slash.

for example, I do

script$url=http://www.stackoverflow.com

then it redirects to:

anotherscript?ip=127.0.0.1&url=http:/www.stackoverflow.com

and the second script chokes on the single-slash.

I realize that preserving a double-slash is the exact opposite of what people usually do with mod_rewrite. Is there a way I can preserve the double-slash?

EDIT: Solution found with Gumbo's help.

RewriteCond %{THE_REQUEST} ^GET\ (.*)/script\$([^\s]+) 
RewriteRule ^script\$(.*) anotherscript?ip=%{REMOTE_ADDR}&%2 [L]

I had to add that (.*) in front of /script on the RewriteCond, once I did that it got rid of the 404 errors and then it was just a matter of passing the matches through.

nerdabilly
  • 1,248
  • 4
  • 15
  • 34

2 Answers2

2

Try this rule:

RewriteCond %{THE_REQUEST} ^GET\ /script\$([^\s]+)
RewriteRule ^script\$.+ anotherscript?ip=%{REMOTE_ADDR}&%1 [L]

See Diggbar modrewrite- How do they pass URLs through modrewrite? for the explanation.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • this is causing a 404 not found – nerdabilly May 20 '09 at 18:49
  • Hope you didn’t forget the obligatory `RewriteEngine on`. – Gumbo May 20 '09 at 19:13
  • Well it works for me. Have you already tried the internal logging feature for debugging? See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteloglevel – Gumbo May 20 '09 at 19:29
  • I've enabled logging and all it says is "[Thu May 21 12:58:45 2009] [error] [client 127.0.0.1] File does not exist: C:/(path)" it looks like its trying to find a file called "script$id=123" instead of parsing that part out. – nerdabilly May 21 '09 at 17:02
  • Try to disable MutliViews. That may cause Apache to look for alternatives before passing the request to mod_rewrite. – Gumbo May 21 '09 at 17:06
  • ok, im not sure that disabling Multiviews did anything but I did notice something: when there is a url in the request, the response is 403 with "(20024)The given path misformatted or contained invalid characters: Cannot map GET " but when there is no URL its a 404 with the File does not exist error. – nerdabilly May 21 '09 at 17:29
  • Have you thought about using another separator? And are there any other rules that could get in conflict with this rule? – Gumbo May 21 '09 at 18:38
  • I actually found a solution that was pretty close to your original suggestion. Thanks for all the help! – nerdabilly May 21 '09 at 18:44
1

I Think there may be something wrong with the first part of your RewriteRule regex

^script\$(.*[\])?

The backslash ( \ ) is used to escape a special character into a litteral one, thus you are actually trying to match a closing bracket ( ] ), is that intended ?

try this

RewriteRule ^script\$(.*)? anotherscript?ip=%{REMOTE_ADDR}&$1 [L]
duckyflip
  • 16,189
  • 5
  • 33
  • 36
  • good catch, I removed that part and it still worked but the double-slash is still being converted to a single-slash. – nerdabilly May 20 '09 at 19:29