【问题标题】:URL Rewrite - Remove .html extensionURL 重写 - 删除 .html 扩展名
【发布时间】:2022-03-04 19:27:35
【问题描述】:

所以想法是像这样从每个页面中删除 .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 用户团结起来!

标签: .net html iis url-rewriting web-config


【解决方案1】:

这个解决方案最终对我有用:

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

【讨论】:

  • 不错的瑞亚诺!我会投票给你 - 但过去我们被告知要投票......
  • 不幸的是,这也会删除 .css、.js 和其他扩展名
【解决方案2】:

使用 IIS 7.x 的重写模块:
http://www.techrepublic.com/blog/webmaster/url-rewriting-with-iiss-url-rewrite-module/710

虽然我已经尝试过了,但我从来没有得到真正的规则集来自动没有有一个规则每个页面名称。

+1 给任何可以阐明这一点的人!

【讨论】:

    【解决方案3】:

    在此处添加来自Remove HTML extension with web config permanently 的答案。这对我来说非常有效,使用 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>
    

    【讨论】:

    猜你喜欢
    • 2019-04-21
    • 2012-02-24
    • 2015-03-20
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2017-09-09
    • 2013-09-15
    • 1970-01-01
    相关资源
    最近更新 更多