mod_rewrite的:传递路径和放大器;查询字符串URL作为参数 [英] mod_rewrite: Pass the path & query string URL as a parameter

查看:152
本文介绍了mod_rewrite的:传递路径和放大器;查询字符串URL作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用mod_rewrite为pretty重写的URL由一个Spring 2.5应用程序所支持的格式。

I'm using mod_rewrite to rewrite pretty URLs to a form supported by a Spring 2.5 application.

e.g. /category/cat1?q=x   =>  /controller?category=cat1&q=x

不过从我的控制,我想知道的请求来自原始URL(这样我就可以在需要生成一个链接)。一般是需要所有网页的这种做法,因此很难很难code。

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.

我如何可以访问我的控制器原始路径+查询字符串?

我已经使用$ 0包含完整的路径尝试,但不包括查询字符串。我不能只追加路径,因为这会导致路径的一些部分被添加作为参数 /分类/ CAT1类= CAT1和放大器的查询字符串; Q = X 注意增加不必要的&放大器;类别= CAT1 参数,这将导致URL到从浏览器发送不再匹配

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=x Note the addition of the unwanted &category=cat1 parameter, this causes the URL to no longer match that sent from the browser.

我希望mod_rewrite的将让我引用的完整URL和连接code将其作为一个参数,所以我的规则可能看起来像:

I'm hoping mod_rewrite will let me reference the full URL and encode it as a parameter so my rule could look like:

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

重要的部分是价值&放大器; _originalUrl 这应该是%2Fcategory%2Fcat1%3Fsearch%3DX 这在其转义的版本是 /分类/ CAT1?q = X (这是从浏览器发送原始请求URL)。

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).

任何建议表示欢迎,在此先感谢!

Any suggestions welcome, thanks in advance!

推荐答案

查询可以ONY与<一个测试href=\"http://httpd.apache.org/docs/2.2/mod/mod%5Frewrite.html#rewritecond\"><$c$c>RewriteCond因为<一个href=\"http://httpd.apache.org/docs/2.2/mod/mod%5Frewrite.html#rewriterule\"><$c$c>RewriteRule确实只测试 URL路径。请参见乔纳森·范伯格的例子的如何做到这一点。

The query can ony be tested with RewriteCond since RewriteRule does only test the URL path. See Jonathan Feinberg’s example how to do that.

但你也可以只设置 QSA 的标记和旧的查询就会自动附加到新的:

But you could also just set the QSA flag and the old query gets automatically appended to the new one:

RewriteRule ^/category/([^/]+)$ /controller?category=$1 [QSA]


修改在回答这个问题您的评论:如果你想获得初始请求的URI路径和查询,你需要从的请求行 THE_REQUEST 的变量):

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):

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.

这篇关于mod_rewrite的:传递路径和放大器;查询字符串URL作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆