永久删除带有Web配置的HTML扩展

Zac*_*ack 7 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>
Run Code Online (Sandbox Code Playgroud)

它工作正常并删除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重定向,但这不会起作用,因为这里有很多文件,所以我必须使用不同的不同网址的301规则.

请指教.

谢谢

fee*_*per 21

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

<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>
Run Code Online (Sandbox Code Playgroud)

  • 完美..节省了大量时间 (2认同)