【问题标题】:.htaccess exclude images from rewrite condition.htaccess 从重写条件中排除图像
【发布时间】:2016-03-21 06:41:09
【问题描述】:

我一直在尝试将 png 和 jpg 文件排除在重写条件之外。目前,我有一组图片被显示为损坏的图像。导致错误的原因是我的 .htaccess 中的重写规则。该规则会覆盖文件夹的 URL,因此它将在 URL 中显示为不同的名称,而不是 /volley,它显示为 JeepVolleyballChampionship。如果目录不存在,我试图摆脱重定向到主页的最后一部分,但似乎没有任何效果。我还尝试从重写目录的重写块中排除扩展名为 png 和 jpg 的文件...

我假设我没有正确实施该排除。我的页面仍然可以正常加载,除了损坏的图像。您能否提供有关我在此块中做错了什么的见解?

   <IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>

这里提供了我的整个 .htaccess:

#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

DirectorySlash Off
RewriteEngine on  
#try to make it so that /volley/ is eliminated from folders and name changes but it is still under this structure
RewriteRule ^volley$ /volley/index.php [L,E=LOOP:1]

RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/$ /volley [R=301,L]   

RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/index.php$ /volley [R=301,L]     

#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>

#non-existent folder to default root
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ / [L,QSA]
</IfModule>

【问题讨论】:

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


    【解决方案1】:

    jpg 和 jpeg 在这里被视为单独的扩展名。在第 3 行添加 jpeg。如果您的扩展名是“jpeg”,它可能会解决您的问题。 它将如下所示:

    RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|\.gif)$ [NC]
    

    【讨论】:

    • 实际上我在排除行中包含了更多文件,但我遇到问题的大多数文件都是扩展名 .png。
    • 如果所有其他扩展都可以正常工作,则通过与它们进行比较来检查 .png 文件的路径。
    【解决方案2】:

    您不应该多次输入“RewriteEngine on”,并且您还需要注意规则的顺序。在我的脑海中,我认为你应该需要的是:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/volley /JeepVolleyballChampionship [R=301]
    

    !-f 条件将阻止图像被重写(连同所有其他文件),只要它们存在,这是排除所有现有文件(如 js、css 等)的规范方式。

    那么规则中的模式只会重写以/volley开头的URL,所以不需要条件来匹配——事实上,规则模式是在条件之前检查的,所以匹配比匹配快一切,然后检查条件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      相关资源
      最近更新 更多