Search code examples
c#asp.netasp.net-mvcrazor

How can I avoid duplicate content in ASP.NET MVC due to case-insensitive URLs and defaults?


Edit: Now I need to solve this problem for real, I did a little more investigation and came up with a number of things to reduce duplicate content. I posted detailed code samples on my blog: Reducing Duplicate Content with ASP.NET MVC

First post - go easy if I've marked this up wrong or tagged it badly :P

In Microsoft's new ASP.NET MVC framework it seems there are two things that could cause your content to be served up at multiple URLs (something which Google penalize for and will cause your PageRank to be split across them):

  • Case-insensitive URLs
  • Default URL

You can set the default controller/action to serve up for requests to the root of your domain. Let's say we choose HomeController/Index. We end up with the following URLs serving up the same content:

  • example.com/
  • example.com/Home/Index

Now if people start linking to both of these then PageRank would be split. Google would also consider it duplicate content and penalize one of them to avoid duplicates in their results.

On top of this, the URLs are not case sensitive, so we actually get the same content for these URLs too:

  • example.com/Home/Index
  • example.com/home/index
  • example.com/Home/index
  • example.com/home/Index
  • (the list goes on)

So, the question... How do I avoid these penalties? I would like:

  • All requests for the default action to be redirected (301 status) to the same URL
  • All URLs to be case sensitive

Possible?


Solution

  • Bump!

    MVC 5 Now Supports producing only lowercase URLs and common trailing slash policy.

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.LowercaseUrls = true;
            routes.AppendTrailingSlash = false;
         }
    

    Also on my application to avoid duplicate content on different Domains/Ip/Letter Casing etc...

    http://yourdomain.example/en

    https://yourClientIdAt.YourHostingPacket.example/

    I tend to produce Canonical URLs based on a PrimaryDomain - Protocol - Controller - Language - Action

    public static String GetCanonicalUrl(RouteData route,String host,string protocol)
    {
        //These rely on the convention that all your links will be lowercase!
        string actionName = route.Values["action"].ToString().ToLower();
        string controllerName = route.Values["controller"].ToString().ToLower();
        //If your app is multilanguage and your route contains a language parameter then lowercase it also to prevent EN/en/ etc....
        //string language = route.Values["language"].ToString().ToLower();
        return String.Format("{0}://{1}/{2}/{3}/{4}", protocol, host, language, controllerName, actionName);
    }
    

    Then you can use @Gabe Sumner's answer to redirect to your action's canonical URL if the current request URL doesn't match it.