htaccess 文件规则
我的 .htaccess 中有这样的规则:
RewriteRule ^art_(.*).htm$ art_item.php?id=$1
并且链接如下: art_1.html => art_item.php?id=1
现在我想添加子链接,如下所示: art_1/5.html => art_item.php?id=1&id2=5
RewriteRule ^art_(.*)/(.*).htm$ art_item.php?id=$1&id=$2
我尝试过,但服务器抛出 404 错误。怎么了?
I have such rule in my .htaccess:
RewriteRule ^art_(.*).htm$ art_item.php?id=$1
And link like this: art_1.html => art_item.php?id=1
Now I want to add sublink which looks like: art_1/5.html => art_item.php?id=1&id2=5
RewriteRule ^art_(.*)/(.*).htm$ art_item.php?id=$1&id=$2
I tried that, but server throws 404 error. What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要注意规则的顺序。
错误的顺序:
正确的顺序:
这是因为
(.*)
也匹配(.*)/(.*)
,因为/
是与.
匹配的任何字符。其次:
.htm
和.html
之间存在差异,因此要么小心,要么只使用\.html?
,它与两者匹配。First, you need to be careful about the order of the rules.
wrong order:
correct order:
This is because
(.*)
also matches(.*)/(.*)
, since the/
is any character matched by.
.Second: there is a difference between
.htm
and.html
, so either be careful or just use\.html?
, which matches both.假设
id
是数字(最好尽可能精确),以下内容应涵盖它:注意:
id2
用于第二次捕获The following should cover it assuming
id
is numeric (it's best to be as exact as possible):Note:
id2
for the second capture