html - 永久删除带有 web 配置的 HTML 扩展

标签 html iis redirect web-config

我正在尝试使用 web.config 从页面中删除 html 扩展名。下面是我在 web.config 文件中使用的代码

<rewrite>
  <rules>
    <rule name="rewrite html">
      <match url="(.*)$" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).html" />
      </conditions>
      <action type="Rewrite" url="{R:1}.html" />
    </rule> 
  </rules>
</rewrite>

它工作正常并删除了 html 扩展名,但是这里似乎有 2 个问题:

  1. 当我输入“斜杠”时,它不起作用,并显示未找到的错误。例如:http://example.com/my-page/ 现在它不会工作,但我把 http://example.com/my-page 然后它会很好地工作,所以我希望他们两个都能工作

  2. 另一个问题是 .html 页面仍在打开。例如,如果我将页面打开为 http://example.com/my-page.html 它也可以工作,但我希望它转换为 http://example.com/my-page 自动,我知道我可以为此使用 301 重定向,但是由于这里有很多文件,所以这不起作用,所以我必须对不同的 URL 使用不同的 301 规则。

    <

请指教。

谢谢

最佳答案

URLRewrite 2.0 规则(将此部分插入 system.webServer 节点内)替换 url 中的 .html:

<rewrite>
    <rules>
        <rule name="Hide .html ext">
            <match ignoreCase="true" url="^(.*)"/>
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                <add input="{REQUEST_FILENAME}.html" matchType="IsFile"/>
            </conditions>
            <action type="Rewrite" url="{R:0}.html"/>
        </rule>
        <rule name="Redirecting .html ext" stopProcessing="true">
            <match url="^(.*).html"/>
            <conditions logicalGrouping="MatchAny">
                <add input="{URL}" pattern="(.*).html"/>
            </conditions>
            <action type="Redirect" url="{R:1}"/>
        </rule>
    </rules>
</rewrite>

关于html - 永久删除带有 web 配置的 HTML 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19631493/

相关文章:

php - 访问 PHP 页面 URL?

php - DOMDocument saveHTML 错误地编码 href 属性

asp.net - IIS 8.5 不使用 applicationHost.config?

c# - 能否使用 CER 来保证永远不会调用 finalize?

php - htaccess 中主机名的重定向规则

php - header ("location") 导致 [500] 内部服务器错误?

html - 删除点击区域的蓝色边框

php - 如何将 css 应用于 src 为 PHP 的 iframe?

javascript - JQuery image.mapster 与其他 jquery 版本冲突

c# - 是否可以在 IIS HttpModule 中修改 HttpRequest POST 的内容?