我正在尝试使用 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 个问题:
当我输入“斜杠”时,它不起作用,并显示未找到的错误。例如:
http://example.com/my-page/
现在它不会工作,但我把http://example.com/my-page
然后它会很好地工作,所以我希望他们两个都能工作另一个问题是
<.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/