Htaccess 重写匹配相同的模式

Htaccess rewrite match same patterns

如何使两个重写都像 http://example.com/something.html http://example.com/videos/something/1.html 一样工作,它总是匹配 download.php 而不是 video.php

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L] 
RewriteBase /
RewriteRule ^category/(.+)$ category.php?q=
RewriteRule ^videos/(.+)/(.+).html$ video.php?q=&page= 
RewriteRule ^(.+).html$ download.php?id=

试试这个:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L] 
RewriteBase /
RewriteRule ^category/(.+)$ category.php?q=
RewriteRule ^videos/(.+?)/(.+?).html$ video.php?q=&page= 
RewriteRule ^(.+).html$ download.php?id=

+ 是贪婪运算符。这就是 download.php 总是匹配的原因。

您可以将这些规则设置为:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L,NE] 

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^category/(.+)$ category.php?q= [L,QSA,NC]

RewriteRule ^videos/([^/]+)/([^/.]+)\.html$ video.php?q=&page= [L,QSA,NC]

RewriteRule ^(.+)\.html$ download.php?id= [L,QSA,NC]