永久删除带有网络配置的 HTML 扩展 [英] Remove HTML extension with web config permanently
问题描述
我正在尝试使用 web.config 从页面中删除 html 扩展.下面是我在 web.config 文件中使用的代码
I am trying to remove html extension from pages using web.config. Below is the code i am using in web.config file
<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 扩展名,但是这里似乎有两个问题:
it is working fine and removing html extension, however there seems to be 2 problems here :
当我输入斜线"时,它不起作用并给我未找到的错误.例如:
http://example.com/my-page/
现在它不起作用,但我把http://example.com/my-page
然后它会正常工作,所以我希望他们俩都能工作
When i put 'slash' it does not work and gives me not found errors. For example:
http://example.com/my-page/
now it will not work, but I puthttp://example.com/my-page
then it will work fine, so i would like to both of them to work
另一个问题是 .html
页面仍在打开.例如,如果我将页面打开为 http://example.com/my-page.html
它也可以工作,但我希望它转换为 http://example.com/my-page
自动,我知道我可以为此使用 301 重定向,但这将不起作用,因为这里有很多文件,所以我必须对不同的 URL 使用不同的 301 规则.
Other problem is that .html
pages are still opening. For example, if I open the page as http://example.com/my-page.html
it is also working but I want it to convert to http://example.com/my-page
automatically, I know I can use 301 redirects for this but that will not work as there are many of files here, so I have to use different 301 rules for different URLs.
请多多指教.
谢谢
推荐答案
URLRewrite 2.0 rule (insert this part inside system.webServer
node) 从 url 替换 .html
:
URLRewrite 2.0 rule (insert this part inside system.webServer
node) that replaces .html
from 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>
这篇关于永久删除带有网络配置的 HTML 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!