【问题标题】:htaccess redirect but exclude imageshtaccess 重定向但排除图像
【发布时间】:2017-04-28 12:39:01
【问题描述】:

我正在尝试使用 htaccess 重定向到文件,但无法显示图像。

我想将任何请求重定向到 mydomain.com 或 www.mydomain.com 到 www.mydomain.com/test.html

这是我的 htaccess 文件的内容:

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/test.html
RewriteCond %{REQUEST_URI} !=*\.png$ [NC]
RewriteRule .* /test.html

我的 test.html 文件中有这行代码:

<img src="image.png" alt="My Image">

但是图像根本不显示,我只是得到一个损坏的图像。我也尝试使用图像的绝对路径,同样的事情。

【问题讨论】:

    标签: image .htaccess redirect


    【解决方案1】:

    尝试用以下内容替换您的 .htaccess 文件

    # Turn rewriting on
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    
    #if the request does not end with test.html or is not a .png
    RewriteCond %{REQUEST_URI} !(test\.html|\.png)$ [NC]
    # Rewrite it to test.html
    RewriteRule .* /test.html  [L]
    

    【讨论】:

      【解决方案2】:

      这将排除各种类型的图像

      #if the request does not end with .gif,.png or jpg 
      RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
      RewriteRule ^index.html$ index.php [NC] 
      

      【讨论】:

        【解决方案3】:

        试试:

        RewriteCond %{REQUEST_URI} !\.png$ [NC]
        

        不熟悉!. 前缀。检查RewriteCond Docs

        【讨论】:

        • 是的,抱歉,我不知道那个错字是怎么出现的。但是我的 htaccess 文件中的代码是正确的,我刚刚编辑了我的第一篇文章。
        • 来自 RewriteCond 文档,当使用 = 时,模式被视为纯文本,因此这可能不起作用
        猜你喜欢
        • 1970-01-01
        • 2012-09-17
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 2021-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多