locked
Show user friendly URLs and hide file extensions RRS feed

  • Question

  • User1793652459 posted

    Hello,

    I have a simple website created in VS2017 based on an empty template that only contains HTML, CSS and some javascript, there is no server side code. It basically displays some static data.  The two HTML pages it contains, show their file extention like information.html and services.html in the URLs. How do I remove the extension so users should only see and access them as http://www.my.company.com/information and http://www.my.company.com/services?

    Is this a coding task or I could change it when I deploy and set up this website on an IIS server?

    Thanks

    Tuesday, May 21, 2019 8:14 PM

Answers

  • User-821857111 posted

    You can do this on the server if you have  the URL Rewrite module installed. Add a web.config file to the site and add this to the <configuration> section:

    <system.webServer>
    <rewrite>
    <rules>
        <rule name="extensionless" stopProcessing="true">
              <match url="(.*)\.html$" />
              <action type="Redirect" url="{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="removeextension" enabled="true">
            <match url=".*" negate="false" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
                </conditions>
                <action type="Rewrite" url="{R:0}.html" />
        </rule>
    </rules>
    </rewrite>
    </system.webServer>

    See this for more info: https://www.hanselman.com/blog/RedirectingASPNETLegacyURLsToExtensionlessWithTheIISRewriteModule.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 21, 2019 8:45 PM