【问题标题】:Logical AND in htaccess modrewrite?htaccess mod_rewrite中的逻辑与?
【发布时间】:2010-01-10 20:44:08
【问题描述】:

这是我的 htaccess 文件

RewriteBase /

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

RewriteCond %{REQUEST_URI} !^/chat/
RewriteCond %{REQUEST_URI} !^/m/
RewriteCond %{REQUEST_URI} !^/__admin/
RewriteCond %{REQUEST_URI} !^/gzip_headers.php
RewriteCond %{REQUEST_URI} !^/phpfreechat/
RewriteCond %{REQUEST_URI} !^/_temp/


RewriteRule ^.+\.php$ index.php [L]

RewriteRule ^.*\.css gzip_headers.php [L]
RewriteRule ^.*\.js gzip_headers.php [L]

RewriteRule ^classifieds/ /index.php [L]

RewriteCond %{REQUEST_URI} !^/movies/.
RewriteRule ^movies/ /index.php [L]

RewriteCond %{REQUEST_URI} !^/games/.
RewriteRule ^games/ /index.php [L]

RewriteRule ^jntu/ /index.php [L]
RewriteRule ^news/ /index.php [L]

我的想法基本上是,

  • 将所有内容转发到 public_html/index.php(某些目录除外)
  • 将所有 js 和 css 转发到 gzip 文件,(我这样做主要是因为我不只是 gzip 压缩它们,而是在 tha phpfile 中压缩)
  • 问题是当我从子目录加载图像时,它们也被重定向到 index.php,因此只需为这些目录创建条件并将图像存储在其中,如 RewriteCond %{REQUEST_URI} !^/games/.

我想让这样的事情变得简单

  • 将所有内容转发到 index.php(顶部的某些条件除外)
  • 将 css 和 js 转发到 gzip 文件
  • 仅当它们存在时才立即加载图像和闪存以及其他一些 MIME 类型。 (jpg|gif|png|swf|flv|mp4|3gp|mp3|zip|rar|exe)

我猜类似于逻辑 AND REQUEST_URI 和 -f 标志

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    试试这些规则:

    RewriteCond %{HTTP_HOST} =example.com [NC]
    RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
    
    RewriteRule .*\.(js|css)$ gzip_headers.php [L]
    
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .+\.(jpg|gif|png|swf|flv|mp4|3gp|mp3|zip|rar|exe)$ - [L]
    
    RewriteCond %{REQUEST_URI} !^/(gzip_headers|index)\.php$
    RewriteCond %{REQUEST_URI} !^/(chat|m|__admin|phpfreechat|_temp)/
    RewriteRule ^.+\.php$ index.php [L]
    

    【讨论】:

    • 这太棒了 :) 正是我想要的,我在最后添加了这个,希望它是正确的RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} ^/(movies|games|jntu|news|classifieds)/ RewriteRule ^. /index.php [L]
    【解决方案2】:

    如果您的规则仅重定向以“.php”结尾的 URI,我不确定为什么要重定向您的图像。这应该从规则中排除所有其他文件扩展名。

    我也不确定您所说的需要“逻辑与”是什么意思。当 RewriteRule 之前有许多 RewriteCond 行时,这些条件会被“与”在一起,并且只有在它们都为真时才应用规则。

    您不能使用 modrewrite 来检查文件是否存在并说“如果文件存在,则不应用任何规则,只需提供文件”。

    我认为最好的解决方案是使用一个名为“static”或“images”的顶级目录,您可以在其中放置所有文件并将其排除在规则之外,或者使用更广泛的匹配规则。

    因此,例如,您可以将“静态”或“图像”设为特殊目录名称,并从规则中排除任何包含 .*/images/.* 的 url。然后/something/images/image.jpg/something/else/images/image.jpg 都将被排除并提供文件。

    另一种 hacky 方法是从 PHP 提供文件。因此,在 PHP 中,您可以将 $_SERVER['REQUEST_URI'] 转换为文件名并查看它是否存在。如果是这样,您可以将文件内容写入 PHP 输出流,尽管这不如将其留给 Apache 效率高,实际上我真的不推荐它。

    但就像我之前说的,如果您的规则只匹配以 .php 结尾的文件,那么您的图像不应该被重定向。我会首先弄清楚为什么会发生这种情况。有一种方法可以打开 mod_rewrite 的调试日志记录,但您必须使用 Google 搜索。

    【讨论】:

    • 嗨,图像没有显示,因为 php 的重写规则被这些 RewriteCond %{REQUEST_URI} !^/games/ 覆盖。 RewriteRule ^games/ /index.php [L]
    猜你喜欢
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多