apache mod_rewrite:将路径和查询字符串 URL 作为参数传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1882694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mod_rewrite: Pass the path & query string URL as a parameter
提问by Richard Paul
I'm using mod_rewrite to rewrite pretty URLs to a form supported by a Spring 2.5 application.
我正在使用 mod_rewrite 将漂亮的 URL 重写为 Spring 2.5 应用程序支持的表单。
e.g. /category/cat1?q=x => /controller?category=cat1&q=x
However from my controller I want to know the original URL the request came from (so I can generate a link if required). This approach is needed generically across all pages so it is difficult to hard code.
但是,从我的控制器中,我想知道请求来自的原始 URL(因此我可以根据需要生成链接)。所有页面都需要这种方法,因此很难进行硬编码。
How can I access the original path + query string from my controller?
如何从我的控制器访问原始路径 + 查询字符串?
I have tried using $0 to include the full path but this doesn't include the query string. I can't just append the path and the query string as this would result in some parts of the path being added as parameters /category/cat1?category=cat1&q=xNote the addition of the unwanted &category=cat1parameter, this causes the URL to no longer match that sent from the browser.
我曾尝试使用 $0 来包含完整路径,但这不包含查询字符串。我不能只附加路径和查询字符串,因为这会导致路径的某些部分被添加为参数/category/cat1?category=cat1&q=x注意添加不需要的&category=cat1参数,这会导致 URL 不再匹配从浏览器发送的 URL。
I'm hoping mod_rewrite will let me reference the full URL and encode it as a parameter so my rule could look like:
我希望 mod_rewrite 能让我引用完整的 URL 并将其编码为参数,这样我的规则就可以如下所示:
RewriteRule /category/(.+)
/controller?category=&_originalUrl=${escape:/controller?category=cat1&_originalUrl=%2Fcategory%2Fcat1%3Fsearch%3Dx&search=x
}?${escape:<original query string>}
[QSA]
Using my original example the end result passed through to my controller would be:
使用我的原始示例,传递给我的控制器的最终结果将是:
RewriteRule ^/category/([^/]+)$ /controller?category= [QSA]
The important part is the value of &_originalUrlwhich should be %2Fcategory%2Fcat1%3Fsearch%3Dxwhich in its unescaped version is /category/cat1?q=x(the original request URL that was sent from the browser).
重要的部分是&_originalUrlwhich的值应该是%2Fcategory%2Fcat1%3Fsearch%3Dxwhich 在其未转义版本中是/category/cat1?q=x(从浏览器发送的原始请求 URL)。
Any suggestions welcome, thanks in advance!
欢迎任何建议,提前致谢!
回答by Gumbo
The query can ony be tested with RewriteCondsince RewriteRuledoes only test the URL path. See Jonathan Feinberg's example how to do that.
查询只能测试,RewriteCond因为RewriteRule只测试URL 路径。请参阅 Jonathan Feinberg 的示例如何做到这一点。
But you could also just set the QSAflagand the old query gets automatically appended to the new one:
但您也可以只设置QSA标志,旧查询会自动附加到新查询:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ ([^\s]+)
RewriteRule ^/category/([^/]+)$ /controller?category=&_originalUrl=%1 [QSA]
Edit In response to your comment to this question: If you want to get the initial requested URI path and query, you need to extract it from the request line(THE_REQUESTvariable):
编辑 回应您对此问题的评论:如果您想获取初始请求的 URI 路径和查询,您需要从请求行(THE_REQUEST变量)中提取它:
RewriteCond %{query_string} ^q=([^&]+)
RewriteRule ^/category/(cat1)$ /controller?category=&q=%1
But in most languages there is an environment variable with the very same information.
但是在大多数语言中,都有一个具有相同信息的环境变量。
回答by Jonathan Feinberg
You have to capture the query string in an initial, separate step.
您必须在初始的单独步骤中捕获查询字符串。
ReWriteRule /category/(cat1)?q=(x) /controller?category=&q= [E=FOO:....]
etc.
等等。
回答by Nic Wise
Is there a host header you can use? Sorry for being so vague, but last time I had to do this was using PHP (urgh, dont ask), and I think thats how we did it - eg REQUEST_URI (http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html)
是否有可以使用的主机标头?很抱歉这么含糊,但上次我不得不这样做是使用 PHP(呃,不要问),我认为我们就是这样做的 - 例如 REQUEST_URI(http://www.askapache.com/htaccess/mod_rewrite-变量-cheatsheet.html)
You also may be able to SET a host header in the rewrite (http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)
您也可以在重写中设置主机标头(http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)
eg:
例如:
##代码##(and no, I'm so totally NOT a mod-rewrite ninja)
(不,我完全不是一个改写的忍者)

