Can mod_rewrite preserve a double slash?

南笙酒味 提交于 2019-12-20 04:16:08

问题


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:


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.




回答2:


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]


来源:https://stackoverflow.com/questions/889435/can-mod-rewrite-preserve-a-double-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!