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

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

问题描述

我正在使用 mod_rewrite 将漂亮的 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(因此我可以在需要时生成链接).所有页面都需要这种方法,因此很难进行硬编码.

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 来包含完整路径,但这不包含查询字符串.我不能只附加路径和查询字符串,因为这会导致路径的某些部分被添加为参数 /category/cat1?category=cat1&q=x 注意添加不需要的 &category=cat1 参数,这会导致 URL 不再匹配从浏览器发送的 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 并将其编码为参数,这样我的规则就可以如下所示:

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,在它的未转义版本中是 /category/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!

推荐答案

该查询只能通过 RewriteCondRewriteRule 只测试 URL 路径.请参阅 Jonathan Feinberg 的示例.

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:传递路径&amp;查询字符串 URL 作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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