How to fix canonical trailing slash with a 301 redirect on IIS.


A ThatsIT Solutions Tutorial

If a web application uses a URL that’s does not directly map to the file and directory structure on the file system of the web server, you must make sure that the trailing slash symbol is used consistently in the URL. Search engine rankings can be lowered for a website if it is accessed by using a URL that’s uses the trailing slash symbol inconsistently. Use the rule template to create a redirect rule that will enforce the use of the trailing slash symbol in the URL.

You should decide if you want a trailing slash after your URL or not, having decided you should make sure that all your links point to the correct version of your URL to avoid unnecessary 301 redirects, remember that 301 redirects leak link juice. I suggest using no trailing URL's and people that may link to you but writing out a URL freehand will likely not include a trailing slash.

Using IIS manager

The easiest way to fix canonical issues on Microsoft Windows Servers is using the url-rewire add-in for IIS. If you don’t have the URL rewrite tool you can download it using the Web Platform Installer. If you don't have access to the IIS Manager you can place the code at the bottom of this page directly in the web.config file.

  1. Open your IIS Manager and highlight the website you want to apply the rules to.
  2. Double click url-rewrite.
    Url rewrite
  3. Click add rules from the right menu.
  4. Select the append or remove the trailing slash rule.
    SEO rule templates
  5. Select your preferred rule, remove or append trailing slash.
    Canonical trailing slash rule

That’s it all done. You can now look in your web.config and see the results.

In the web.config file

<system.webserver> 
    </rewrite>
        <rules> 
            .........
            <rule name="RemoveTrailingSlashRule1" stopprocessing="true"> 
                <match url="(.*)/$" /> 
                    <conditions> 
                        <add input="{REQUEST_FILENAME}" matchtype="IsDirectory" negate="true" /> 
                        <add input="{REQUEST_FILENAME}" matchtype="IsFile" negate="true" /> 
                    </conditions>
                <action type="Redirect" url="{R:1}" /> 
            </rule>
            .........
        </rules>
    </rewrite>
</system.webServer>