[英]mod_rewrite: Pass the path & query string URL as a parameter
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). 但是,从我的控制器我想知道请求来自的原始URL(所以我可以生成链接,如果需要)。 This approach is needed generically across all pages so it is difficult to hard code.
所有页面通常都需要这种方法,因此很难进行硬编码。
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. 我尝试使用$ 0来包含完整路径,但这不包括查询字符串。 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=x
Note the addition of the unwanted &category=cat1
parameter, this causes the URL to no longer match that sent from the browser. 我不能只追加路径和查询字符串,因为这会导致路径的某些部分被添加为参数
/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=$1&_originalUrl=${escape:$0}?${escape:<original query string>}
[QSA]
Using my original example the end result passed through to my controller would be: 使用我的原始示例,最终结果传递给我的控制器将是:
/controller?category=cat1&_originalUrl=%2Fcategory%2Fcat1%3Fsearch%3Dx&search=x
The important part is the value of &_originalUrl
which should be %2Fcategory%2Fcat1%3Fsearch%3Dx
which in its unescaped version is /category/cat1?q=x
(the original request URL that was sent from the browser). 重要的部分是
&_originalUrl
的值,它应该是%2Fcategory%2Fcat1%3Fsearch%3Dx
,其非转义版本是/category/cat1?q=x
(从浏览器发送的原始请求URL)。
Any suggestions welcome, thanks in advance! 欢迎任何建议,提前谢谢!
The query can ony be tested with RewriteCond
since RewriteRule
does only test the URL path . 查询可以使用
RewriteCond
进行测试,因为RewriteRule
仅测试URL路径 。 See Jonathan Feinberg's example how to do that. 请参阅Jonathan Feinberg的示例,了解如何做到这一点。
But you could also just set the QSA flag and the old query gets automatically appended to the new one: 但您也可以设置QSA标志 ,旧查询会自动附加到新查询:
RewriteRule ^/category/([^/]+)$ /controller?category=$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_REQUEST variable): 编辑响应您对此问题的评论:如果要获取初始请求的URI路径和查询,则需要从请求行 ( THE_REQUEST变量)中提取它:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ ([^\s]+)
RewriteRule ^/category/([^/]+)$ /controller?category=$1&_originalUrl=%1 [QSA]
But in most languages there is an environment variable with the very same information. 但在大多数语言中,都有一个具有相同信息的环境变量。
You have to capture the query string in an initial, separate step. 您必须在初始的单独步骤中捕获查询字符串。
RewriteCond %{query_string} ^q=([^&]+)
RewriteRule ^/category/(cat1)$ /controller?category=$1&q=%1
etc. 等等
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(urgh,不要问),我想我们是怎么做到的 - 例如REQUEST_URI( http://www.askapache.com/htaccess/mod_rewrite- variables-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: 例如:
ReWriteRule /category/(cat1)?q=(x) /controller?category=$1&q=$2 [E=FOO:....]
(and no, I'm so totally NOT a mod-rewrite ninja) (不,我完全不是一个mod-rewrite ninja)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.