您需要将第一条规则移到底部,否则它将与^(\w+)$
模式匹配任何字母数字字符,然后由于[L]
ast 指令而停止处理。正如目前所写的那样,您的其他规则之一将永远匹配,因为该模式将匹配“admin”、“search”或“myusername”。“目录”模式已更改为任何不是正斜杠的字符匹配,这通常比.
通配符更适合 URL 匹配,因为它可能是贪婪的。我通常也喜欢包括一个RewriteBase
。
# Enable mod_rewrite and set the base directory
RewriteEngine on
RewriteBase /
# These are directories (directory/SOMETHING)
RewriteRule ^(admin|def|images|includes|profile|uploads|users)/([^/]*) test.php?f=second&s=$2$3 [NC,L]
# Matches p/SOMETHING
RewriteRule ^p/([^/]*)$ index.php?v2=$1 [NC,L]
# Matches search/SOMETHING/QUERY
RewriteRule ^search/(([^/]*)/)?(.*)$ index.php?v=search&v2=$2&s=$3 [NC,L]
# Matches directory (with or without trailing slash)
RewriteRule ^(faq|privacy|terms|contact|verify)/?$ index.php?v=$1 [NC,L]
# No other rules matched, assume this is a username
RewriteRule ^(\w+)$ test.php?f=first&s=$1 [NC,L]
您可以使用http://htaccess.madewithlove.be/进行测试