• Home
  • Tutorials
  • Development Tools
  • Contact Us

Developing Software

Mastering Software Craftsmanship

How to Improve the SEO of an ASP.NET MVC Website

4th December 2014 by @developingsoft

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?

  1. 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.
  2. Enable Attribute routing by calling the routes.MapMvcAttributeRoutes() method in your RouteConfig class.
  3. 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.

Share this on:

Filed Under: Tutorials Tagged With: ASP.NET MVC, C#

Search

Advertisement

Newsletter

Subscribe now to receive practical tips on how to become a better software developer.

Free - No Spam - 100% Email Privacy

Featured Posts

Abstract Factory Pattern: C# Example Using the Unity Game Engine

23 Software Design Patterns That Will Make You a More Effective Programmer

How to Deploy an ASP.NET Core Website to Ubuntu with Git

How to Run an ASP.NET Core Website in Production on Ubuntu Linux

How to Install the Edimax Wireless nano USB Adapter on Windows IoT Core for Raspberry Pi

How to Convert a Post Title into a Friendly URL (Slug) in C#

How to Convert Markdown to HTML in ASP.NET Core

How to Send an E-Mail with ASP.NET Core and Mailgun

How to Generate a Sitemap in ASP.NET MVC and ASP.NET Core

How to Create an MD5 Hash of a String in C# and Displaying a Gravatar Image

© 2014–2023 Developing SoftwareTerms • Privacy