使用.htaccess重写URL。我应该先放哪条规则?

时间:2015-03-02 05:46:31

标签: php apache .htaccess mod-rewrite url-rewriting

我想改写:

www.domain.com/entry/1www.domain.com/entry/1/titlewww.domain.com/entry.php?id=1

www.domain.com/entry/1/title/page/2www.domain.com/entry.php?id=1&page=2

www.domain.com/entry/1/title/commentwww.domain.com/entry.php?id=1&a=comment

我在.htaccess文件中有以下内容

RewriteRule ^entry/(([0-9]+))(?:/([^/]*))?/?$ ./entry.php?id=$1  
RewriteRule ^entry/([0-9]+)/([^/]+)/page/([0-9]+)$ ./entry.php?id=$1&page=$3 
RewriteRule ^entry/([0-9]+)/([^/]+)/([^/]+)$ ./entry.php?id=$1&a=$3  

它在工作。我也尝试将订单反转为:

RewriteRule ^entry/(([0-9]+))(?:/([^/]*))?/?$ ./entry.php?id=$1  
RewriteRule ^entry/([0-9]+)/([^/]+)/([^/]+)$ ./entry.php?id=$1&a=$3  
RewriteRule ^entry/([0-9]+)/([^/]+)/page/([0-9]+)$ ./entry.php?id=$1&page=$3 

它也在工作。如何执行.htaccess文件中的行?上面哪个版本更正确?谢谢!

1 个答案:

答案 0 :(得分:0)

没关系。但是一定要在每个RewriteRule行的末尾添加[L],以便Apache在成功应用规则后停止,并且不会无休止地循环。

This is also a very useful tool用于测试RewriteRules以及它们是否正常工作。