【问题标题】:Htaccess rewrite match same patternsHtaccess 重写匹配相同的模式
【发布时间】:2015-03-21 09:46:00
【问题描述】:

我怎样才能使两种重写都像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/$1 [R=301,L] 
RewriteBase /
RewriteRule ^category/(.+)$ category.php?q=$1
RewriteRule ^videos/(.+)/(.+).html$ video.php?q=$1&page=$2 
RewriteRule ^(.+).html$ download.php?id=$1

【问题讨论】:

    标签: php regex .htaccess mod-rewrite


    【解决方案1】:

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

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$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=$1 [L,QSA,NC]
    
    RewriteRule ^videos/([^/]+)/([^/.]+)\.html$ video.php?q=$1&page=$2 [L,QSA,NC]
    
    RewriteRule ^(.+)\.html$ download.php?id=$1 [L,QSA,NC]
    

    【讨论】:

    • 喜欢跳过文件部分。
    • 内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。请通过 webmaster@ 联系服务器管理员,告知他们此错误发生的时间,以及您在此错误之前执行的操作。服务器错误日志中可能会提供有关此错误的更多信息。
    • 您收到此 500 错误的具体网址是什么?我已经在我的 Apache 上对此进行了测试,没有发现任何错误。
    【解决方案2】:

    试试这个:

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

    + 是一个贪婪的运算符。这就是为什么download.php 总是匹配的原因。

    【讨论】:

      猜你喜欢
      • 2012-02-18
      • 2012-11-25
      • 2010-11-13
      • 2013-10-09
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      相关资源
      最近更新 更多