CheapASPNETHostingReview.com | Best and Cheap ASP.NET MVC 6 hosting review and comparison. 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 6.
1. Use attribute routing instead of the default route
Attribute Routing is a new feature added to ASP.NET MVC 6 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 6 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:
1 2 3 4 5 6 | [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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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 cheapaspnethostingreview.com redirects to www.cheapaspnethostingreview.com This prevents duplicate content.
The following example shows a rewrite rule that redirect the none www to the www subdomain.
1 2 3 4 5 6 7 8 | <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 ASPHostPortal. The ASPHostPortal.com domain will be redirected to your main domain, preventing more duplicate content.