Did you know that the default route of ASP.NET MVC will produce multiple URL’s that display the same content? And as I am sure you are aware, duplicate content can cause all sorts of SEO problems. In this post, I will show you three steps to prevent duplicate content with ASP.NET MVC 5.
1. Use attribute routing instead of the default route
Attribute Routing is a new feature added to ASP.NET MVC 5 that allows you to manually define each route by placing an attribute on each action.
Why use attribute routing?
Create a new ASP.NET MVC 5 application, and you will find the following URL’s all show the home page:
- http://localhost
- http://localhost/home
- http://localhost/home/
Now I know, that it is unlikely that the search engine will discover the duplicate URL’s, but it is better to make sure only one can be accessed, and this can be done by using attribute routing.
Also, in my experience I have found that using attribute routing instead of the default route makes it easier to debug routing issues.
How to enable attribute routing?
- Remove the default route from your
RouteConfig
. Or even better, replace the default route with a catch all that points to an action that displays 404 not found. - Enable Attribute routing by calling the
routes.MapMvcAttributeRoutes()
method in yourRouteConfig
class. - Manually add the route to each action. For example, assigning
[Route("")]
to the index action of the home controller will fix the issue above.
2. Add or remove the trailing forward slash from your URL’s
There is still a slight issue. For example, with the following attribute applied to the about page:
[Route("about")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View()
}
The about page can be accessed using the following URL’s:
- http://localhost/about
- http://localhost/about/
You can fix this by adding a rewrite rule to your Web.config file. For example, the following rule will remove the trailing forward slash from all URL’s:
<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>
Just remember to set the AppendTrailingSlash
to false
in the RouteConfig
class so that the routing system generates URL’s that have no trailing forward slash.
3. Redirect none www to www
It’s best practice to have either the www redirect to the none www or the www redirect to the www. For example, on this site talksharp.com redirects to www.developingsoftware.com. This prevents duplicate content.
The following example shows a rewrite rule that redirect the none www to the www subdomain.
<rule name="Rewrite domain requests" stopProcessing="true" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?([.a-zA-Z0-9]+)$" />
<add input="{HTTP_HOST}" pattern="^www\.yourdomain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.yourdomain.com/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
The other good thing about the rule above, is it will redirect all domains to the primary domain. For example, if you host your site on Azure. The azurewebsites.net domain will be redirected to your main domain, preventing more duplicate content.