帮助解决冲突的mod-rewrite

时间:2011-02-08 22:16:22

标签: apache .htaccess mod-rewrite

我希望能够在这两个mod重写上使用foo这是我现在拥有的并且工作正常:

#Links to categorys
    RewriteRule foo/(.*)/ foo.php?cat=$1
    RewriteRule foo/(.*) foo.php?cat=$1 

#Links to categorys with pagination    
    RewriteRule fooo/(.*)/(.*)/ foo.php?cat=$1&id=$2
    RewriteRule fooo/(.*)/(.*) foo.php?cat=$1&id=$2

但是真的想要这样做:

#Links to categorys
    RewriteRule foo/(.*)/ foo.php?cat=$1
    RewriteRule foo/(.*) foo.php?cat=$1

#Links to categorys with pagination    
    RewriteRule foo/(.*)/(.*)/ foo.php?cat=$1&id=$2
    RewriteRule foo/(.*)/(.*) foo.php?cat=$1&id=$2

因为两者相互关联(相同的页面)只是一个额外的分页id的情况,但如果我没有额外的“o”它冲突,所以给我一个404

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

问题来自于这条规则:

RewriteRule foo/(.*)

...将匹配符合您的分页规则的任何内容,并将首先执行此操作。最简单的解决方案是仅反转规则顺序并进行一些小的更改:

#Links to categorys with pagination
RewriteRule foo/([^/]*)/([^/]+)/? foo.php?cat=$1&id=$2

#Links to categorys
RewriteRule foo/([^/]*)/? foo.php?cat=$1

此外,如果您的脚本可以处理没有值传递的id,您可以将它全部压缩为一个规则:

# Links to categories with optional pagination
RewriteRule foo/([^/]*)(?:/([^/]+))?/? foo.php?cat=$1&id=$2