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

IIS管理器:如何隐藏URL文件夹路径仅显示网站主域名

解决IIS隐藏深层文件夹路径的重写规则问题

前提确认

首先确保你的IIS服务器已经安装了URL重写模块:

  • 打开IIS管理器,查看网站的「模块」列表,确认有RewriteModule;
  • 如果没安装,从服务器管理器的「添加角色和功能」里找到「Web服务器(IIS)」→「Web服务器」→「应用程序开发」,勾选「URL重写」完成安装。

配置重写规则

找到网站根目录下的web.config文件(无则新建),替换或添加以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- 设置默认文档,确保根路径优先匹配重写规则 -->
    <defaultDocument>
      <files>
        <clear />
        <add value="index.html" />
      </files>
    </defaultDocument>

    <rewrite>
      <rules>
        <!-- 规则1:访问根路径时,内部转发到深层index.html(地址栏不变) -->
        <rule name="Internal Rewrite to Deep Index" stopProcessing="true">
          <match url="^$" />
          <action type="Rewrite" url="/folder1/folder2/folder3/index.html" />
        </rule>

        <!-- 规则2:禁止直接访问深层路径,重定向到根路径 -->
        <rule name="Redirect Deep Path to Root" stopProcessing="true">
          <match url="^folder1/folder2/folder3/(.*)" />
          <action type="Redirect" url="/" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

关键注意事项(避免500错误)

  • 确保网站根目录的web.config有IIS_IUSRS的读取权限;
  • 深层文件夹folder1/folder2/folder3也要给IIS_IUSRS访问权限,否则内部转发会报错;
  • 测试前清空浏览器缓存,避免旧的缓存规则干扰;
  • 如果你的网站用了ASP.NET等框架,不要覆盖原有web.config的其他配置,只把<rewrite>和<defaultDocument>部分合并进去。

验证效果

  1. 访问https://webtool.test.com/,地址栏保持不变,页面显示深层index.html的内容;
  2. 尝试直接访问https://webtool.test.com/folder1/folder2/folder3/index.html,会自动重定向到根路径,地址栏显示根URL;
  3. 试错其他深层路径(比如/folder1/folder2/xxx.html),会返回404(因为重定向规则只匹配指定深层路径,其他未授权路径直接拦截)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 22:57:16