【问题标题】:How can I make Apache rewrite not break an image?如何让 Apache 重写而不破坏图像?
【发布时间】:2018-07-25 22:05:31
【问题描述】:

我在 example.com 上有一个非常简单的 html 页面。它仅由以下index.html 组成:

<html>
<body style="background-color:black;">
<img src="images/picture1.jpg" alt="example" style="width:40%;">
</html>

当我转到 example.com 时,页面加载完全符合预期,但我想这样做,以便将对 example.com 的任何请求重定向到此首页(因此 example.com/catfood 将用户带到 example.com )。为此,我将以下内容添加到.htaccess

RewriteEngine On
RewriteCond %{REQUEST_URI} !=/
RewriteRule ^ / [R=301]

它可以正确重定向,但会破坏图像。我相信这种情况正在发生,因为对 example.com/images/picture1.jpg 的请求现在被重定向到索引页面,但我不确定我需要添加到我的 .htaccess 中以允许全面重定向同时发生仍在首页上加载此图像。

【问题讨论】:

    标签: apache .htaccess http webserver


    【解决方案1】:

    添加另一个 RewriteCond,表示该规则仅在请求未指向现有文件时适用。

    RewriteCond   %{REQUEST_FILENAME} !-f
    

    所以:

    RewriteEngine On
    RewriteCond   %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !=/
    RewriteRule ^ / [R=301]
    

    https://httpd.apache.org/docs/current/mod/mod_rewrite.html

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 2021-10-16
      • 2020-06-26
      • 2011-04-06
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多