【问题标题】:Exception with .htaccess (image broken).htaccess 异常(图像损坏)
【发布时间】:2011-10-21 04:52:46
【问题描述】:

我正在使用:

RewriteRule ^(.*)$ index.php?abrir=$1
RewriteRule ^(.*)/(.*)/(.*)$ index.php?abrir=$1&id=$2&pagina=$3

我有图片链接:

/data_livro/capa_livro/af58e898f91509b.jpg

如何添加显示图像的例外?它坏了……

我的完整 .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^/]+)$ index.php?abrir=$1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?abrir=$1&id=$2&pagina=$3

RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

ErrorDocument 400 index.php
ErrorDocument 401 index.php
ErrorDocument 403 index.php
ErrorDocument 404 index.php
ErrorDocument 504 index.php
ErrorDocument 505 index.php
DirectoryIndex index.php

【问题讨论】:

    标签: .htaccess friendly-url


    【解决方案1】:

    如果我理解正确,在您的 RewriteRules 之前添加以下内容应该可以解决问题:

    RewriteCond %{REQUEST_FILENAME} !-f
    

    但是,我还建议修改您的 RewriteRules 看起来像:

    RewriteRule ^([^/]+)$ index.php?abrir=$1
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?abrir=$1&id=$2&pagina=$3
    

    【讨论】:

    • 也许可以尝试删除最后一个 RewriteRule (图像文件扩展名)。我列出的 RewriteRules/RewriteCond 可以按照您想要的方式在我的服务器上正常工作,所以我不确定为什么它们不会在您的服务器上运行。另外,请尝试确保您启用了 FollowSymLinks。 (您可以通过将 Options +FollowSymLinks 添加到您的 .htaccess 文件中来做到这一点。)
    【解决方案2】:

    我发现在重写规则中使用.* 过于笼统。我倾向于使用更具体的字符类:

    RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?abrir=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?abrir=$1&id=$2&pagina=$3
    

    这不会重写任何带有点的请求,因此特定文件名(/styles/my.css、/images/example.jpg)会被忽略。另外,我喜欢添加/? 以允许使用可选的斜杠,但这是个人喜好:-)。

    【讨论】:

      猜你喜欢
      • 2014-07-01
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 2023-01-01
      相关资源
      最近更新 更多