URL Rewrite - Remove .html extension

穿精又带淫゛_ 提交于 2019-12-07 04:01:15

问题


So the idea is to remove the .html extension from each page like so...

www.website.com/File.html > www.website.com/File
www.website.com/Folder/File.html > www.website.com/Folder/File

Now I've managed to do this using a URL Rewrite, but it means having to write a rewrite for each page, which is time consuming, not efficient and impractical say if the website is more than 20 pages.

Is there a way to do this by writing just one or two rewrites in the web.config?


回答1:


This solution worked for me in the end:

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



回答2:


Use the rewrite module for IIS 7.x:
http://www.techrepublic.com/blog/webmaster/url-rewriting-with-iiss-url-rewrite-module/710

Although I have tried this, I've never gotten the actual rule set to do it automatically without having a rule per page name.

+1 to anyone who can shed light on this!



来源:https://stackoverflow.com/questions/10210936/url-rewrite-remove-html-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!