6

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?

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
Ryano
  • 2,125
  • 3
  • 20
  • 28

3 Answers3

9

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> 
Ryano
  • 2,125
  • 3
  • 20
  • 28
1

Adding the answer from Remove HTML extension with web config permanently here. This worked very nicely for me, using the URL Rewrite 2.1 module. You edit this into the applicationHost.config file. Thanks to 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>
Jon R
  • 836
  • 11
  • 9
0

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!

Barrie Reader
  • 10,647
  • 11
  • 71
  • 139