我的 htaccess 文件中有此重写规则:
RewriteRule ^([a-zA-Z0-9-]+)/?$ /home.php?id=$1 [L,QSA]
将home.php?id=123
重写为www.domain.com/123
我怎么能这样做:
www.domain.com/services/123
仍将转到home.php?id=123
,但仅限于某些网址
我想说:
home.php?id=1
home.php?id=2
home.php?id=3
home.php?id=4
我希望1和3看起来像www.domain.com/services/1
和www.domain.com/services/2
然后2和4看起来像www.domain.com/2
和www.domain.com/4
但如果用户转到www.domain.com/services/2
或www.domain.com/services/4
,他们只会找不到网页?
答案 0 :(得分:0)
# The square brackets denote a range of allowed values
# so for /services I've specified either a 1 or a 3 is valid
RewriteRule ^services/([13])/?$ /home.php?id=$1 [L,QSA]
# And for requests without /services I've specified that just 2 or 4
# are valid
RewriteRule ^[24]/?$ /home.php?id=$1 [L,QSA]
# If you want all URLs that don't have /services but do have a number
# eg domain.com/3, domain.com/2345 and domain.com/95234234 then you
# can use this:
RewriteRule ^([0-9]+)/?$ /home.php?id=$1 [L,QSA]
原始RewriteRule
允许domain.com/hello
重定向到domain.com/home.php?id=hello