ASP.NET Site Move HTTP to HTTPS, Refresh Google Indexing

When a site is moved from HTTP to HTTPS which is already indexed by Google, we must add a new property for the HTTPS URL in Goolge search console. When Google tries to index the site and it finds HTTP URL indexed for the same page, it will mark the HTTPS pages as canonical. We should use “301 Moved Permanently” to tell Google that HTTP URLs are moved to HTTPS and Google should prefer HTTPS over HTTP.

Here is a sample code to be used in an ASP.NET web application for 301 moved status

if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith("http://someweb.com"))
{
    string newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace("http://someweb.com", "https://www.someweb.com");
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", newUrl);
}

if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith("http://www.someweb.com"))
{
    string newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace("http://www.someweb.com", "https://www.someweb.com");
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", newUrl);
}

if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith("https://someweb.com"))
{
    string newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace("https://someweb.com", "https://www.someweb.com");
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", newUrl);
}

Leave A Comment

Your email address will not be published.