【问题标题】:Apache2 redirect to maintenance page - missing imagesApache2 重定向到维护页面 - 缺少图像
【发布时间】:2017-07-08 04:03:21
【问题描述】:

我想暂时将所有流量从 example.com 重定向到维护页面并返回 503 代码。我创建了maintenance.html页面:

<!DOCTYPE html>
<html>
<head>
    <title>We'll be back!</title>
    <link href="css/main.css" rel="stylesheet" media="screen">
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body style="background-color: #red">
<div>
    <div id="header-row" class="row">
        <img src="/images/header-logo.png" alt="loogo" class="img-responsive center-block">
    </div>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/main.js"></script>
</body>
</html>

我把html页面、css、js和images文件夹放到文档根文件夹下。然后我在 apache2.2 配置文件中添加了以下几行:

RewriteEngine On

#prevent from recursiv redirection
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteCond %{REQUEST_URI}  !(js|css|images)$
RewriteCond %{REQUEST_FILENAME} !.*\.p
RewriteCond %{REQUEST_FILENAME} !-f

#503 and redirect
RewriteRule ^.*$  /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.h

#do not cache
Header Set Cache-Control "max-age=0, no-store"

所有调用都被重定向到maintenance.html 页面,但没有显示图像。我试图通过添加来修复它:

    RewriteCond %{REQUEST_FILENAME} !-d

但随后重定向停止工作。奇怪的是,添加 RewriteCond %{REQUEST_URI} !(js|css|images)$ 修复了缺少样式的问题,但图像仍然被重定向。我做错了什么?


编辑 1:

我检查了浏览器如何搜索 header-logo.png。地址是http://example.com/images/header-logo.png,所以我添加了用于测试以下规则:

  RewriteCond %{REQUEST_URI} !^/images/header-logo\.png$

但这也没有帮助....

【问题讨论】:

    标签: apache .htaccess maintenance


    【解决方案1】:

    我做错了什么?

    条件

    RewriteCond %{REQUEST_URI} !(js|css|images)$
    

    匹配jscssimages 中的所有请求结束。这当然包括所有*.js(Javascript),*.css(样式)。但它不包括图像文件,因为这些文件以.png.jpg 结尾,而不是images

    要为 目录 设置条件,您不能在末尾锚定模式,而是用斜杠将其括起来,例如

    RewriteCond %{REQUEST_URI} !/(?:js|css|images)/
    

    我不明白的是,为什么会发生重定向,因为

    RewriteCond %{REQUEST_FILENAME} !-f
    

    应该已经足够了。但它也可以防止重写任何现有的其他文件。


    一个最小的规则集可能是

    RewriteCond %{REQUEST_URI} !^/maintenance\.html$
    RewriteCond %{REQUEST_URI} !/(?:js|css|images)/
    RewriteRule ^  - [R=503,L]
    
    ErrorDocument 503 /maintenance.html
    

    【讨论】:

    • 我试了这一套,图像仍然不存在,Chrome->Inspect 显示每个图像“503(服务暂时不可用)”。
    • 然后还有其他事情发生,因为在我的测试环境中图像加载正确。
    • 不幸的是,它可能是任何东西,其他地方的 .htaccess 文件,主/虚拟主机配置中的任何指令,您的网络提供商所做的一些设置。
    • 可能是images 目录拼写错误或无法访问(访问权限)?
    • 我昨天检查过了;/-rw-rw-r-- 1 ubuntu devs 22K Feb 17 09:33 header-logo.png
    【解决方案2】:

    你可以这样使用它:

    ErrorDocument 503 /maintenance.html
    
    #do not cache
    Header Set Cache-Control "max-age=0, no-store"
    
    RewriteCond %{REQUEST_URI} !/(js|css|images)/ [NC]
    RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|ico|tiff|css|js|p[a-z]*)$ [NC]
    RewriteRule !^maintenance\.html$ /maintenance.html [L,NC]
    

    【讨论】:

    • 我试过了,结果出人意料,重定向不起作用,主页缺少样式和图像(只有纯文本)。
    • 您应该拥有 js/css 的完整路径,例如 &lt;script src="/js/bootstrap.min.js"&gt;&lt;/script&gt; 或者您可以在页面 HTML 的 &lt;head&gt; 部分下方添加此路径:&lt;base href="/" /&gt;
    • 我尝试了更新版本,但它导致页面关闭错误:服务暂时不可用。也许它是由“!”引起的在重写规则中?
    • 我试过没有“!”并且重定向停止工作;/
    • 试试我更新的规则,确保你没有改变任何东西。还要说明您使用什么 URL 进行测试。
    【解决方案3】:

    已解决:老实说,我不知道为什么会这样,但问题是 apache 无法访问 images 文件夹。一旦我将它重命名为 img,一切都开始正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 2011-12-07
      • 2014-05-15
      • 2011-06-03
      • 2013-01-18
      • 2015-08-11
      • 2021-11-14
      相关资源
      最近更新 更多