0

我想将用户个人资料网址作为domain.com/their_username

所以我需要所有字母数字字符,除了少数要重写

这是我想出的,但它不起作用

RewriteRule ^(\w+)$ test.php?f=first&s=$1 [NC,L]

RewriteRule ^((admin|def|images|includes|profile|uploads|users)(.*)) test.php?f=second&s=$2$3 [NC,L]

RewriteRule ^p/(.*)$ index.php?v2=$1 [NC,L]

RewriteRule ^search/((.*)/)?(.*)$ index.php?v=search&v2=$2&s=$3 [NC,L]

RewriteRule ^(faq|privacy|terms|contact|verify)$ index.php?v=$1 [NC,L]
4

1 回答 1

1

您需要将第一条规则移到底部,否则它将与^(\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/进行测试

于 2013-11-09T14:36:50.670 回答