.htaccess而是一个范围使用2个选项

时间:2015-09-30 03:33:03

标签: regex apache .htaccess mod-rewrite

我的.htaccess文件中包含此代码:

Options +SymLinksIfOwnerMatch
RewriteEngine On    # Turn on the rewriting engine

RewriteRule    ^([a-z]+)/([a-z]+)?$    index.php?lang=$1&loc=$2    [NC,L]

RewriteRule    ^([a-z]+)/([a-z]+)/restaurants/?$    all_rest.php?lang=$1&loc=$2    [NC,L]

正如您所看到的,变量langloc可以是([a-z]+)范围内的任何内容,但我不需要总范围我只需要单词:enchinlang变量,hkkwloc变量。

我试过这个:

RewriteRule    ^([en][chin])/([hk][kw])/restaurants/?$    all_rest.php?lang=$1&loc=$2    [NC,L]

但我不知道为什么只适用于hk kw y无效en chin

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您正在使用与个别字符匹配的字符集。因此,[hk][kw]会匹配这些:hk, hw, kk, kw。你想要这个:

RewriteRule ^(en|chin)/(hk|kw)/?$ index.php?lang=$1&loc=$2 [NC,L]
RewriteRule ^(en|chin)/(hk|kw)/restaurants/?$ all_rest.php?lang=$1&loc=$2 [NC,L]