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

前端 未结 7 1138
无人及你
无人及你 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

    I was working on this as well. I will obviously defer to ScottGu on this. I humbly offer my solution to this problem as well though.

    Add the following code to global.asax:

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        // If upper case letters are found in the URL, redirect to lower case URL.
        if (Regex.IsMatch(HttpContext.Current.Request.Url.ToString(), @"[A-Z]") == true)
        {
            string LowercaseURL = HttpContext.Current.Request.Url.ToString().ToLower();
    
            Response.Clear();
            Response.Status = "301 Moved Permanently";
            Response.AddHeader("Location",LowercaseURL);
            Response.End();
        }
    }
    

    A great question!

提交回复
热议问题