【问题标题】:Mod_rewrite not combining a dynamic subdirectory rewrite and a static oneMod_rewrite 不结合动态子目录重写和静态重写
【发布时间】:2017-06-24 03:37:09
【问题描述】:

我有以下 .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ user.php?username=$1 [QSA]
    RewriteRule ^tag/(.*)$ tag.php?tag=$1
</IfModule>

我需要涵盖以下用例:

  1. http://example.com/foo -> http://example.com/user.php?username=foo
  2. http://example.com/about -> http://example.com/about/index.php
  3. http://example.com/标签/栏 -> http://example.com/tag.php?tag=

当前的 mod_rewrite 规则涵盖情况 1 和 2(about/index.php 是真实位置)。我很难让他们覆盖案例 3。

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    通过重新排序规则并关闭MultiViews 选项来实现它。同时跳过所有重写规则中的文件和目录:

    Options -MultiViews
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteRule ^tag/(.+)$ tag.php?tag=$1 [L,NC,QSA]
    
    RewriteRule ^(.+)$ user.php?username=$1 [L,QSA]
    

    【讨论】:

    • 哦,你是救生员,解决了这个问题 - 感谢你整理我的整个文件。如果你有时间,你能解释一下在这种情况下关闭MultiViews 的作用吗?
    • 选项MultiViews(见httpd.apache.org/docs/2.4/content-negotiation.html)由运行之前 mod_rewriteApache's content negotiation module使用,并使Apache服务器匹配文件的扩展名。因此,如果 /file 是 URL,那么 Apache 将提供 /file.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 2013-01-30
    相关资源
    最近更新 更多