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

如何去除HTML网站URL末尾的斜杠?

解决HTML网站URL末尾自动添加斜杠的问题(IIS环境)

你遇到的是IIS的默认行为:当访问一个存在的目录(比如about文件夹)时,服务器会自动重定向到带斜杠的版本(/about/),所以只修改页面里的a标签无法解决,必须通过服务器配置调整。

针对你的网站结构,直接修改web.config添加URL重写规则即可解决,步骤如下:

  1. 确认IIS已安装URL重写模块
    如果你的服务器还没装这个模块,需要先在IIS管理器中安装(通过Web平台搜索“URL重写”即可)。

  2. 更新web.config配置
    替换或添加以下规则到web.config的<system.webServer>节点下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- 移除目录末尾的斜杠,永久重定向 -->
        <rule name="Remove trailing slash" stopProcessing="true">
          <match url="(.*)/$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
          </conditions>
          <action type="Redirect" url="{R:1}" redirectType="Permanent" />
        </rule>
        <!-- 让不带斜杠的目录指向对应的index.html -->
        <rule name="Rewrite directory to index" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
          </conditions>
          <action type="Rewrite" url="{R:1}/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

规则说明

  • 第一条规则:匹配所有末尾带斜杠的目录URL,将其永久重定向到不带斜杠的版本,同时排除对文件的重写(避免影响真实文件的访问)。
  • 第二条规则:当访问不带斜杠的目录(比如/about)时,服务器内部重写到该目录下的index.html,用户看到的URL保持/about不变,但能正常加载页面内容。
  1. 测试前清除浏览器缓存
    因为浏览器可能缓存了之前的重定向规则,测试前先清除缓存,再访问/about就能看到URL不再自动添加斜杠。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 21:28:21