【问题标题】:Rewrite URLs with .htaccess but ignore specific Directories使用 .htaccess 重写 URL,但忽略特定目录
【发布时间】:2012-02-10 14:06:46
【问题描述】:

我在网站上有一个目录:

我有一个 .htaccess 文件。

我希望它采用这样的任何 URL:

并将其改写为:

引用这些目录的任何 URL 除外:

我有第一部分工作,但无法告诉它排除某些目录中的文件:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]

更新:

这是一个非常基本和简单的意义:

RewriteRule ^css - [L,NC]
RewriteRule ^javascript - [L,NC]
RewriteRule ^images - [L,NC]
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]

但我确定有更优雅的解决方案?

【问题讨论】:

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


    【解决方案1】:

    我认为您要查找的关键字是 RewriteCond。它与您最终得到的非常相似。

    RewriteCond %{REQUEST_URI} !^(css|javascript|images)
    RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
    

    【讨论】:

      【解决方案2】:

      您可以使用RewriteCond 检查是否存在实际文件...至少,当我希望 CSS、Javascript 和图像等内容可访问时(无需重定向请求),我将其用于网站.)

      这是我使用的行:

      # only rewrite if the requested file doesn't exist
      RewriteCond %{REQUEST_FILENAME} !-s
      

      让我知道这是否适合你!

      【讨论】:

        猜你喜欢
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 2019-09-09
        • 2023-03-20
        • 1970-01-01
        • 2013-10-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多