Force IIS to redirect requests to URLs with, or without, a trailing slash to enforce consistent URL structure, can be good for SEO.
Remove Trailing Slash
To always remove an ending slash from the URL, add the following to web.config’s <rules> section:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < system.webServer > < rewrite > < rules > < rule name = "Remove trailing slash" stopProcessing = "true" > < match url = "(.*)/$" /> < conditions > < add input = "{REQUEST_FILENAME}" matchType = "IsFile" negate = "true" /> < add input = "{REQUEST_FILENAME}" matchType = "IsDirectory" negate = "true" /> </ conditions > < action type = "Redirect" redirectType = "Permanent" url = "{R:1}" /> </ rule > </ rules > </ rewrite > </ system.webServer > |
For SEO purposes, this type of redirect should be Permanent (301).
Enforce Trailing Slash
To always add a slash to end of a URL if it doesn’t already end with one, add the following to web.config’s <rules> section:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < system.webServer > < rewrite > < rules > < rule name = "Add trailing slash" stopProcessing = "true" > < match url = "(.*[^/])$" /> < conditions > < add input = "{REQUEST_FILENAME}" matchType = "IsFile" negate = "true" /> < add input = "{REQUEST_FILENAME}" matchType = "IsDirectory" negate = "true" /> </ conditions > < action type = "Redirect" redirectType = "Permanent" url = "{R:1}/" /> </ rule > </ rules > </ rewrite > </ system.webServer > |
For SEO purposes, this type of redirect should be Permanent (301).