URL重写:冲突的重写规则

时间:2017-01-30 18:20:35

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

我尝试使用网址重写做三件事:

  1. 删除.php文件扩展名
  2. 我的帖子网址如下所示:mydomain.co.uk/resources/example-post默认为mydomain.co.uk/resources/post.php?s=example-post
  3. 我的类别存档URLS如下所示:mydomain.co.uk/resources/category/example-category默认为mydomain.co.uk/resources/archive.php?cat=example-category
  4. 这就是我目前在.htaccess文件中的内容:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^\.]+)$ $1.php [L,QSA]
    
    RewriteRule ^resources/([a-zA-Z0-9-/]+)$ /resources/post.php?s=$1 [L]
    
    RewriteRule ^resources/category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ /resources/archive.php?cat=$1&page=$2 [L]
    RewriteRule ^resources/category/([a-zA-Z0-9-/]+)$ /resources/archive.php?cat=$1 [L]
    

    它做了前两件事。但不是第三个。

    如果我删除

    RewriteRule ^([^\.]+)$ $1.php [L,QSA]
    

    然后它完成了第三件事,但不是前两件事。

    任何想法会发生什么?如何让它一次完成所有这三件事?

1 个答案:

答案 0 :(得分:0)

找到解决方案。

只需交换订单,我现在有了这个:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L,QSA]

RewriteRule ^resources/category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ /resources/archive.php?cat=$1&page=$2 [L]
RewriteRule ^resources/category/([a-zA-Z0-9-/]+)$ /resources/archive.php?cat=$1 [L]

RewriteRule ^resources/([a-zA-Z0-9-/]+)$ /resources/post.php?s=$1 [L]

工作正常。