htaccess Mod Rewrite忽略URL的一部分

时间:2013-12-18 22:17:32

标签: .htaccess mod-rewrite

我的 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/1www.domain.com/services/2

然后2和4看起来像www.domain.com/2www.domain.com/4

但如果用户转到www.domain.com/services/2www.domain.com/services/4,他们只会找不到网页?

1 个答案:

答案 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