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

前端 未结 7 1164
无人及你
无人及你 2020-12-24 03:22

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 duplicat

7条回答
  •  自闭症患者
    2020-12-24 04:12

    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.com/en

    https://yourClientIdAt.YourHostingPacket.com/

    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.

提交回复
热议问题