htaccess文件规则

时间:2011-05-17 20:23:02

标签: apache http .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错误。怎么了?

2 个答案:

答案 0 :(得分:2)

首先,您需要注意规则的顺序。

错误的订单:

RewriteRule ^art_(.*)\.html$ art_item.php?id=$1
RewriteRule ^art_(.*)/(.*)\.html$ art_item.php?id=$1&id2=$2

正确的顺序:

RewriteRule ^art_(.*)/(.*)\.html$ art_item.php?id=$1&id2=$2
RewriteRule ^art_(.*)\.html$ art_item.php?id=$1

这是因为(.*)也匹配(.*)/(.*),因为/ 匹配. 的任何字符。

第二:.htm.html之间存在差异,因此要么小心,要么只使用与{2}匹配的\.html?

答案 1 :(得分:1)

以下内容应涵盖假设id为数字(最好尽可能准确):

RewriteRule ^art_(\d+)\.htm$ art_item.php?id=$1
RewriteRule ^art_(\d+)/(\d+)\.htm$ art_item.php?id=$1&id2=$2

注意: id2进行第二次捕获