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

IIS配置强制跳转至https://www.example.com并移除index.php问题

IIS URL重定向/重写配置修正方案

原配置存在的问题

  1. 规则顺序错误:重定向规则应优先于重写规则执行,否则重写会先触发,导致重定向不生效
  2. 覆盖范围不全:仅处理了example.com到www.example.com的跳转,未覆盖http://www.example.com转HTTPS、https://example.com转HTTPS+WWW的场景
  3. 目录排除正则不严谨:原规则中*/folder1*的匹配模式会误匹配包含folder1字符串的其他路径,且缺少统一的多目录排除逻辑
  4. 未处理index.php的显式URL跳转:需求是移除URL中的index.php,但原配置仅做了路由重写,未对直接访问index.php的请求做跳转

修正后的完整配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <defaultDocument>
      <files>
        <add value="default.php"/>
      </files>
    </defaultDocument>
    <rewrite>
      <rules>
        <!-- 1. 强制跳转所有请求到 https://www.example.com -->
        <rule name="Force HTTPS and WWW" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAny">
            <!-- 匹配非HTTPS请求 -->
            <add input="{HTTPS}" pattern="off" />
            <!-- 匹配非WWW的域名 -->
            <add input="{HTTP_HOST}" pattern="^example\.com$" />
          </conditions>
          <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
        </rule>

        <!-- 2. 移除URL中的index.php(显式访问时跳转) -->
        <rule name="Remove Index.php" stopProcessing="true">
          <match url="^index\.php/(.*)$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/(folder1|folder2)/" negate="true" />
          </conditions>
          <action type="Redirect" url="/{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="Remove Index.php Root" stopProcessing="true">
          <match url="^index\.php$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/(folder1|folder2)/" negate="true" />
          </conditions>
          <action type="Redirect" url="/" redirectType="Permanent" />
        </rule>

        <!-- 3. 路由重写(排除指定目录,将非文件/目录请求转发到index.php) -->
        <rule name="Rewrite to Index.php" stopProcessing="true">
          <match url="^(.*)$" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <!-- 排除folder1和folder2目录下的所有请求 -->
            <add input="{REQUEST_URI}" pattern="^/(folder1|folder2)/" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

配置说明

  • 强制HTTPS+WWW规则:通过MatchAny逻辑,只要是非HTTPS或者非WWW域名的请求,都跳转到https://www.example.com,确保所有入口统一
  • 移除index.php规则:分两种情况处理——根目录的index.php跳转到根路径,带路径的index.php/xxx跳转到/xxx,同时排除指定目录
  • 路由重写规则:仅当请求不是真实文件/目录,且不在排除目录内时,才转发到index.php处理,保证静态资源和指定目录不受影响
  • 所有跳转使用Permanent(301):利于搜索引擎收录,避免重复内容问题

验证步骤

  1. 保存配置到站点根目录的web.config文件
  2. 确保IIS已安装URL重写模块(若未安装,需通过服务器管理器添加)
  3. 测试以下场景:
    • http://example.com → https://www.example.com
    • https://example.com → https://www.example.com
    • http://www.example.com → https://www.example.com
    • https://www.example.com/index.php → https://www.example.com
    • https://www.example.com/index.php/test → https://www.example.com/test
    • https://www.example.com/folder1/test → 直接访问,不触发重写/跳转

内容的提问来源于stack exchange,提问作者G.I.George

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 20:13:37