【问题标题】:htaccess Rerwite rules collidinghtaccess 重写规则冲突
【发布时间】:2021-08-24 04:45:45
【问题描述】:

我必须重写相互冲突的规则。实际上,是第二个规则导致第一个规则不起作用,即使我换了位置。

艺术家音乐.php

From: http://www.example.com/artistmusic?slug=Ben
To: http://www.example.com/Ben/music

artistvideos.php

From: http://www.example.com/artistvideos?slug=Ben
To: http://www.example.com/Ben/videos

上述网址的重写规则。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)$ /artist$2?address=$1 [L]

这就像它应该的那样工作,直到我为下面的一组 URls 添加重写规则。

allsongs.php

From: http://www.example.com/allsongs?standard=popular&request=monthly
To: http://www.example.com/songs/popular/monthly

allartists.php

From: http://www.example.com/allartists?standard=popular&request=monthly
To: http://www.example.com/artists/popular/monthly

重写上面第二组的规则。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /all$1?standard=$2&request=$3

上面的规则有效,但会杀死第一个。 这是什么原因?谢谢。

【问题讨论】:

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


    【解决方案1】:

    使用您显示的示例/尝试,请按以下方式保存您的 htaccess 规则文件。请确保在测试您的 URL 之前清除您的浏览器缓存。

    RewriteEngine ON
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^songs/([^/]*)/([^/]*)/?$ allsongs.php?standard=$1&request=$2 [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^artists/([^/]*)/([^/]*)/?$ allartists.php?standard=$1&request=$2 [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/(music|videos)/?$ artist$2.php?slug=$1 [QSA,NC,L]
    


    通用解决方案:如果您不想在新规则中硬编码值,请尝试遵循。确保一次只使用以上或以下 1 个。

    RewriteEngine ON
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ all$1.php?standard=$2&request=$3 [QSA,NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/?$ artist$2.php?slug=$1 [QSA,NC,L]
    

    【讨论】:

    • @Busker McGreen Brian,您能否告诉我这些规则是否对您有用?
    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多