简体   繁体   English

htaccess 3 参数重写规则

[英]htaccess 3 parameters rewrite rule

I know this problem is asked before, but i don't find anything我知道以前有人问过这个问题,但我什么也没找到

i try to pass 3 variable in htaccess the url change normaly but i cant't get the third variable我尝试在 htaccess 中传递 3 个变量 url 更改正常,但我无法获得第三个变量

this is my htaccess file这是我的 htaccess 文件

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-z]+)\/?$ index.php?r=$1 [NC]
RewriteRule ^([a-z]+)/([a-z]+) index.php?r=$1&s=$2 [L]
RewriteRule ^([a-z]+)/([0-9]+) index.php?r=$1&id=$2 [L]
RewriteRule ^([a-z]+)/([0-9]+)/([a-z]+) index.php?r=$1&id=$2&s=$3 [L]
RewriteRule ^([az]+)/([0-9]+) index.php?r=$1&id=$2 [L] RewriteRule ^([az]+)/([0-9]+)/([az]+) index.php?r=$1&id=$2&s=$3 [L]

These 2 rules conflict.这 2 条规则是冲突的。 A URL of the form /abc/123/def is also matched by the first rule, so the second rule is never processed. /abc/123/def形式的 URL 也与第一条规则匹配,因此永远不会处理第二条规则。

You could either:您可以:

  • Include an end-of-string anchor ( $ ) on the first rule pattern, so that it only matches /abc/123 and not /abc/123/def .在第一个规则模式中包含一个字符串结尾锚 ( $ ),以便它只匹配/abc/123而不是/abc/123/def For example:例如:

     RewriteRule ^([az]+)/([0-9]+)$ index.php?r=$1&id=$2 [L]
  • OR, reverse these two directives, so the rule for 3 parameters takes priority:或者,颠倒这两个指令,因此 3 个参数的规则优先:

     RewriteRule ^([az]+)/([0-9]+)/([az]+) index.php?r=$1&id=$2&s=$3 [L] RewriteRule ^([az]+)/([0-9]+) index.php?r=$1&id=$2 [L]

You should probably include end-of-string anchors on all your patterns, otherwise, they are likely to match too much.您可能应该在所有模式中包含字符串结束锚点,否则它们可能会匹配太多。 eg.例如。 /abc/123/def/for/bar/baz.jpg is matched by the 3rd and 4th rule without an end-of-string anchor. /abc/123/def/for/bar/baz.jpg由第 3 条和第 4 条规则匹配,没有字符串结束锚点。 If you add end-of-string anchors then the filesystem conditions could probably be removed altogether.如果您添加字符串结尾的锚点,那么文件系统条件可能会被完全删除。

As @IMSoP noted in comments, those two conditions (ie. RewriteCond directives) only apply to the first RewriteRule that follows.正如@IMSoP 在评论中指出的那样,这两个条件(即RewriteCond指令)仅适用于随后的第一个RewriteRule The first rule is unlikely to match a real file anyway, so the conditions aren't really doing anything currently.无论如何,第一条规则不太可能匹配真实文件,因此条件目前并没有真正做任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM