views:

403

answers:

2

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.

+1  A: 

Try this rule:

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

See http://stackoverflow.com/questions/831355/diggbar-modrewrite-how-do-they-pass-urls-through-modrewrite/869017#869017 for the explanation.

Gumbo
this is causing a 404 not found
nerdabilly
Hope you didn’t forget the obligatory `RewriteEngine on`.
Gumbo
nope, it's there.
nerdabilly
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
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
Try to disable MutliViews. That may cause Apache to look for alternatives before passing the request to mod_rewrite.
Gumbo
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
Have you thought about using another separator? And are there any other rules that could get in conflict with this rule?
Gumbo
I actually found a solution that was pretty close to your original suggestion. Thanks for all the help!
nerdabilly
+1  A: 

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
good catch, I removed that part and it still worked but the double-slash is still being converted to a single-slash.
nerdabilly