URL Rewrite - Remove .html extension

10,557

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> 
Share:
10,557
Ryano
Author by

Ryano

Junior Web Developer

Updated on June 23, 2022

Comments

  • Ryano
    Ryano almost 2 years

    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?

  • Barrie Reader
    Barrie Reader about 12 years
    Nice one Ryano! I would vote you up - but we have been told off about voting up in the past...
  • Phil C
    Phil C over 8 years
    Unfortunately, this also removes .css, .js, and other extensions
  • Jon R
    Jon R over 3 years
    Improved my answer at: stackoverflow.com/a/62501711/12508260