Apache mod rewrite RewriteCond regex帮助需要

4uqofj5v  于 2023-08-07  发布在  Apache
关注(0)|答案(1)|浏览(143)

Apache mod_rewrite
我有一个请求URL,如下所示

http://11.22.33.44/somecontext/?acc=&quot:1234554321&quot:

字符串
我正在编写一个Apache RewriteCond指令,其中我希望提取出现在两个&quot:之间的数字。因此,从&quot:1234554321&quot:中,我想提取1234554321这个数字(数字序列)包含在&quot:&quot:之间,这是唯一一个get参数,请求字符串以acc=开头,以:结尾
我尝试用以下所有方法编写指令

RewriteCond %{QUERY_STRING} ^acc=\&quot:([0-9])&quot:$
RewriteCond %{QUERY_STRING} ^acc=\&quot\:([0-9])\&quot\:$
RewriteCond %{QUERY_STRING} acc=\&quot\:([0-9])\&quot\:
RewriteCond %{QUERY_STRING} &quot:([0-9])&quot:
RewriteCond %{QUERY_STRING} \&quot\:([0-9])\&quot\:
RewriteCond %{QUERY_STRING} ^acc=([^&quot:]+)$
RewriteCond %{QUERY_STRING} ^acc=\&quot\:(d+)\&quot\:$
RewriteCond %{QUERY_STRING} ^acc=\&quot\:(\d+)\&quot\:$


我的RewriteRule紧接着这个RewriteCond规则,我希望在我的RewriteRule中使用反向引用%1来使用提取的值。
你能建议正确的RewriteCond指令吗?

wkftcu5l

wkftcu5l1#

因为我是mod_rewrite的新手,我参考了http://forum.modrewrite.com/,发现它对揭开mod_rewrite语法的神秘面纱非常有帮助。
不知何故,它只是下面的字符串为我做的工作

RewriteCond %{QUERY_STRING}  ^acc=[a-z&:]+([0-9]+)[a-z&:]+ [NC]

字符串
此条件与URL中的get参数匹配

http://11.22.33.44/somecontext/?acc=&quot:1234554321&quot:


(即以下部分?)由QUERY_STRING变量表示
并按如下方式解析它

  • “^acc=”==>匹配字符串的开头,后跟acc=
  • [a-z&:]+ =字母(无大小写=NC)和字符&和;
  • ([0-9]+)=对于匹配字符串抓取所有数字
  • [a-z&:]+ =字母(无大小写=NC)和字符&和;

抓取的数字在反向引用%1中可用

相关问题