mod_rewrite的:URL中匹配的字符串,来选择其中的正则表达式? [英] mod_rewrite: match string within URL, which regex to chose?

查看:109
本文介绍了mod_rewrite的:URL中匹配的字符串,来选择其中的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用mod_rewrite捕捉到我的网址括号内的字符串,并做了重定向。

我的网址:

东西var_a = A和?var_b =(B)及var_c = C

与正则表达式我.httaccess文件:

  RewriteEngine叙述上
重写规则^ /?+ var_b = \\((。*)\\)$ somedir / $ 1 [R]

我只是想捕捉什么是全面括号,所以我的重定向页面应该是这个样子: somedir / B

测试我在 http://htaccess.madewithlove.be/ 正则表达式,但我没有得到任何比赛。

我不知道我是缺少在这里,即使我尝试更简单的正则表达式,如 + var_b(。*)$ 我没有得到任何比赛。只有当我正则表达式正在寻找在一开始的模式,我得到一个匹配,因此,例如正则表达式东西(。*)$

我是什么在这里失踪?


解决方案

  RewriteEngine叙述在
的RewriteCond%{QUERY_STRING}(^ |&安培;)var_b = \\((*)\\。?)(安培; | $)[NC]
重写规则^ * $ somedir /%2? [R]

原因是,重写规则不接收?X = Y 查询的一部分。在%2 变量是指从最后一个的RewriteCond ,而 $ 2 将引用的方式从这个重写规则。在末prevents查询部分?X = Y 从年底被自动添加的结果。

(^ |&放大器;)(安培; | $)在模式保证 var_b =(B)是完整的参数,而不是它的一部分。如果没有这些,该模式也将匹配?xyzvar_b =(B)?var_b =(B)XYZ 。有了这些,就只匹配 var_b =(B) A = B和;?var_b =(B)及; X = Z 等。

I would like to use mod_rewrite to capture a string within brackets in my URL and do a redirect.

My URL:

something?var_a=A&var_b=(B)&var_c=C

my .httaccess file with the regex:

RewriteEngine on
RewriteRule ^/?.+var_b=\((.*)\)$ somedir/$1 [R]

I just would like to capture what's in between the round brackets, so my redirect should look something like this: somedir/B

I test my regex at http://htaccess.madewithlove.be/ but I get no match.

I don't know what I am missing here, even if I try much simpler regexes, e.g. .+var_b(.*)$ I get no match. Only if my regex was looking for a pattern at the beginning, I get a match, so for example the regex something(.*)$ works.

What am I missing here?

解决方案

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)var_b=\((.*?)\)(&|$) [NC]
RewriteRule ^.*$ somedir/%2? [R]

The reason is that RewriteRule does not receive the ?x=y part of the query. The %2 variable refers to the pattern from the last RewriteCond, while $2 would refer to the pattern from this RewriteRule. The ? at the end prevents the query part ?x=y from being automatically appended at the end of the result.

The (^|&) and (&|$) in the pattern guarantee that var_b=(B) is the complete parameter and not a part of it. Without these, the pattern would also match ?xyzvar_b=(B) or ?var_b=(B)xyz. With these, it will only match ?var_b=(B) or ?a=b&var_b=(B)&x=z etc.

这篇关于mod_rewrite的:URL中匹配的字符串,来选择其中的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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