URL重写 - 去除.html扩展名

6
所以这个想法是从每个页面中去除.html扩展名,就像这样...
www.website.com/File.html > www.website.com/File
www.website.com/Folder/File.html > www.website.com/Folder/File

现在我已经用URL重写实现了这个功能,但这意味着需要为每个页面编写一次重写规则,这样非常耗时、效率低下,如果网站超过20页就不太现实了。
有没有办法只编写一两个web.config中的重写规则来实现这个功能呢?

我也想知道!!! 帮助SO用户团结起来! - Barrie Reader
3个回答

9
这个解决方案最终对我有效:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
    <match url="^(.*)\.(.*)$" />
    <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
    </conditions>
    <action type="Redirect" url="{R:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.(.*)" />
</rule> 

真棒的Ryano!我会投你一票的,但我们被告知过去投票不应该这样做... - Barrie Reader
4
很遗憾,这也会移除 .css、.js 和其他扩展名。 - Phil C

1

在这里添加使用Web配置永久删除HTML扩展名的答案。对我来说,这非常有效,使用了URL Rewrite 2.1模块。您可以将其编辑到applicationHost.config文件中。 感谢https://stackoverflow.com/users/1821692/feeeper

<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>

我在以下链接中改进了我的回答:https://dev59.com/CWIk5IYBdhLWcg3wDaUW#62501711 - Jon R

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接