Redirect URL chữ hoa thường lẫn lộn sang chữ thường ToLower() MVC ASP.net

Thảo luận trong 'Lập Trình Website MVC5 & MVC6' bắt đầu bởi admin, 21/9/18.

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,900
    Đã được thích:
    1,199
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Redirect URL chữ hoa thường lẫn lộn sang chữ thường ToLower() MVC ASP.net sử dụng tập tin Global.asax trong MVC rất quan trọng khi SEO web asp.net. Vì tránh Google hiểu nhầm giữa các liên kết viết hoa và liên kết viết thường khi index web.

    Thông thường web bạn rất dễ bị google index lẫn lộn giữa 2 trạng thái URL viết hoa và URL viết thường. Vậy để google hiểu rõ 2 trạng thái này là 1 thì bạn phải tự Redirect ngay từ lúc đầu khách truy cập vào web sang liên kết viết thường hết để google hiểu rõ web bạn chỉ có 1 liên kết mà không phải 2 liên kết thì bạn đọc tiếp bài dưới nhé.

    Ví dụ: 2 liên kết sau: https://itseovn.com/threads/redirec...n-sang-chu-thuong-tolower-mvc-asp-net.274511/
    Và: https://itseovn.com/threads/REDIREC...N-SANG-CHU-THUONG-TOLOWER-MVC-ASP-NET.274511/

    Mặc định ở web asp.net MVC google sẽ tự hiểu đó là 2 liên kết khác nhau hoàn toàn, google sẽ index 2 liên kết này khác nhau nếu code web bạn gọi 2 liên kết này lẫn lộn trong web.

    redirect-chu-hoa-thuong-lan-lon-sang-chu-thuong-toan-bo-trong-url-aspnet-mvc-global.jpg

    Xử lý:
    • Mở tập tin Global.asax của web bạn chèn đoạn code sau vào nhé:
    Mã:
    protected void Application_BeginRequest()
    {
        //chuyển tất cả url chữ hoa thành chữ thường, VD: itseoVN.com sẽ thành itseovn.com
        var url = Request.Url.ToString();
        if (Regex.IsMatch(url, @"[A-Z]") && Request.HttpMethod != "POST" && !url.Contains("Bundles"))
        {
            Response.Clear();
            Response.Status = "301 Moved Permanently"; //di chuyển vĩnh viễn
            Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
            Response.AddHeader("Location", url.ToLower());
            Response.End();
        }
    }
    Trọn bộ CODE SEO trong Global.asax kết hợp giữa:
    • Tự động redirect toàn bộ chữ hoa thường lẫn lộn trong web sang chữ thường hết
    • Tự động redirect toàn bộ từ www sang không có www
    • Tự động redirect toàn bộ http sang https nếu có
    Mã:
    protected void Application_BeginRequest()
    {
        //Chuyển từ www sang none www vd: www.itseovn.com thành itseovn.com       
        if (!HttpContext.Current.Request.IsLocal && HttpContext.Current.Request.Url.Host.StartsWith("www"))
        {
            var Builder = new UriBuilder(Request.Url);
            Builder.Host = Request.Url.Host.Remove(4);
            Response.RedirectPermanent(Builder.ToString(), true);
        }
    
        //chuyển http to https vd: http://itseovn.com thành https://itseovn.com
        if (HttpContext.Current.Request.IsSecureConnection == false && HttpContext.Current.Request.IsLocal == false)
        {
            Response.RedirectPermanent("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
        }
              
        //chuyển tất cả url chữ hoa thành chữ thường, VD: itseovN.com sẽ thành itseovn.com
        var url = Request.Url.ToString();
        if (Regex.IsMatch(url, @"[A-Z]") && Request.HttpMethod != "POST" && !url.Contains("Bundles"))
        {
            Response.Clear();
            Response.Status = "301 Moved Permanently";
            Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
            Response.AddHeader("Location", url.ToLower());
            Response.End();
        }
    }
    
     
    Cảm ơn đã xem bài:

    Redirect URL chữ hoa thường lẫn lộn sang chữ thường ToLower() MVC ASP.net

    Chỉnh sửa cuối: 21/9/18


Chủ để tương tự : Redirect chữ
Diễn đàn Tiêu đề Date
Lập Trình Website MVC5 & MVC6 Custom 404 error is not redirecting to Global.asax in MVC C# 3/7/20
Lập Trình Website MVC5 & MVC6 Auto Redirect www to non www and http to https using Global in MVC ASP.net 21/9/18
Lập Trình Website MVC5 & MVC6 RedirectToAction with parameter trong MVC C# ASP.NET 2/9/18
Lập Trình Website MVC5 & MVC6 Redirect http sang https sử dụng mô hình MVC trong IIS Asp.net 27/7/18
Lập Trình Website MVC5 & MVC6 Regex.Replace không phân biệt chữ hoa thường trong c# 19/4/20