You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IIS 10中URL Rewrite重写所有请求至根页面及图片加载问题

问题描述

我有一台Web服务器,通过IIS默认网站展示维护页面,要求所有指向该服务器IP的URL(包括带路径的,比如http://a.domain.com/1或/1/2/...)都加载根目录下的页面。当前配置的URL Rewrite规则能加载页面文本,但背景图片无法显示,禁用规则后图片正常,需解决该问题。

当前URL Rewrite配置:

<rewrite>
  <rules>
    <rule name="Rewrite to Root Webpage" enabled="true">
    <match url="^(?!\/$).*" />
    <action type="Rewrite" url="/" appendQueryString="true" />
  <conditions>
  </conditions>
    </rule>
  </rules>
</rewrite>

网页代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Maintenance Mode</title>
<style>
    body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
        height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        background-image: url("Down-Background.png");
        background-repeat: no-repeat;
        background-size: cover;
        background-attachment: fixed;
        color: white;
        text-align: center;
    }

    .container {
        background: rgba(0, 0, 0, 0.5);
        padding: 40px;
        border-radius: 10px;
        max-width: 600px;
        width: 100%;
    }

    h1 {
        font-size: 2.5rem;
        margin-bottom: 20px;
    }

    p {
        font-size: 1.2rem;
        margin-bottom: 20px;
    }

    .footer {
        font-size: 1rem;
        color: #ccc;
    }

</style>
</head>
<body>
<div class="container">
    <h1>We’re busy pushing improvements to give you a flawless user experience</h1>
    <p>We’ll be back soon!</p>
    <div class="footer">Thank you for your patience.</div>
</div>
</body>
</html>
问题原因

当前Rewrite规则匹配了所有非根路径的请求,包括静态资源(比如Down-Background.png)的请求也被重写到根页面,导致浏览器收到的是HTML内容而非图片文件,因此图片加载失败。

解决方案

方案1:修改URL Rewrite规则,排除静态资源

添加条件,仅当请求的文件或目录不存在时才执行重写,确保实际存在的静态资源能正常返回:

<rewrite>
  <rules>
    <rule name="Rewrite to Root Webpage" enabled="true">
      <match url="^(?!\/$).*" />
      <action type="Rewrite" url="/" appendQueryString="true" />
      <conditions>
        <!-- 排除实际存在的文件 -->
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <!-- 排除实际存在的目录 -->
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
    </rule>
  </rules>
</rewrite>

方案2:修改图片引用为绝对路径

将CSS中图片的相对路径改为绝对路径,确保浏览器始终从根目录请求图片:

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-image: url("/Down-Background.png"); /* 修改为绝对路径 */
    background-repeat: no-repeat;
    background-size: cover;
    background-attachment: fixed;
    color: white;
    text-align: center;
}

内容的提问来源于stack exchange,提问作者Floydster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 13:10:13