URL重写 - 删除.html扩展名

Rya*_*ano 6 .net html iis web-config url-rewriting

所以我的想法是从每个页面中删除.html扩展名,就像这样......

www.website.com/File.html > www.website.com/File
www.website.com/Folder/File.html > www.website.com/Folder/File
Run Code Online (Sandbox Code Playgroud)

现在我已经设法使用URL重写来实现这一点,但这意味着必须为每个页面写一个重写,这是耗时的,没有效率,如果网站超过20页则不切实际.

有没有办法在web.config中只写一两次重写?

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

  • 不幸的是,这也删除了.css,.js和其他扩展 (3认同)