URL重写-删除.html扩展名

问题描述 投票:6回答:3

所以想法是像这样从每个页面中删除.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中只编写一两次重写来做到这一点?

.net html iis url-rewriting web-config
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> 

0
投票

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

尽管我已经尝试过了,但是我从来没有获得自动执行此操作的实际规则集没有具有规则per页面名称。

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


0
投票

在这里从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>
© www.soinside.com 2019 - 2024. All rights reserved.