301 Permanent Redirect in ASP.NET MVC

301 Permanent Redirect in ASP.NET MVC, 301 permanent redirect, c# redirect 301, 301 redirects explained, asp.net 301 redirect, asp 301 redirect, permanent redirect 301, 301 permanent, asp redirect 301, html status 301, permanent 301

What is a 301 Permanent Redirect?

A 301 redirect is a permanent redirect from one URL to another. 301 redirects send site visitors and search engines to a different URL than the one they originally typed into their browser or selected from a search engine results page. A 301 redirect is key to maintaining a website's domain authority and search rankings when the site's URL is changed for any reason.

What's the Difference Between Permanent HTML Redirects and other Redirects?

Generally speaking, a 301 permanent redirect in ASP.NET is better for search engine optimization than a temporary redirect because it transfers the inbound links from the redirected domain to the new one, which helps the website maintain its search rankings and prevent any dip in search traffic.

There are few situations where a 302 temporary redirect would be preferable over a 301 permanent redirect - except for when website content needs to be moved temporarily, such as when a site is undergoing maintenance and visitors need to be directed to a different domain to consume their content.

Why Set Up a 301 Permanent Redirect?

The main reasons to set up a 301 permanent redirect are:

  1. To associate common web conventions (http://, www., etc.) with one URL to maximize domain authority (hint: this is the same situation as the scenario we outlined above.)
  2. To rebrand or rename a website with a different URL
  3. To direct traffic to a website from other URLs owned by the same organization

Here's how to implement 301 redirect within ASP.NET MVC custom route handler:

namespace MVCWeb
{
    public class CustomUrlRouteHandler : System.Web.Mvc.MvcRouteHandler
    {
       protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
       {
          //Check if you need to do 301 redirects
          //Get the url to redirect to 
          requestContext.HttpContext.Response.Clear();
          requestContext.HttpContext.Response.Status = "301 Moved Permanently";
          requestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
          requestContext.HttpContext.Response.AppendHeader("Location", "New URL Here"); // Or requestContext.HttpContext.Response.RedirectLocation = "New URL Here";
          requestContext.HttpContext.Response.Flush();
          requestContext.HttpContext.Response.End();

          return base.GetHttpHandler(requestContext);
       }
    }
}

This code can be applied as well in your Base Controller as per the following:

public class WebBaseController : Controller
{
  protected override void OnActionExecuting(ActionExecutingContext context)
  {
    //Check if you need to do 301 redirects
    //Get the url to redirect to 
    requestContext.HttpContext.Response.Clear();
    requestContext.HttpContext.Response.Status = "301 Moved Permanently";
    requestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
    requestContext.HttpContext.Response.AppendHeader("Location", "New URL Here"); // Or requestContext.HttpContext.Response.RedirectLocation = "New URL Here";
    requestContext.HttpContext.Response.Flush();
    requestContext.HttpContext.Response.End();
 
    base.OnActionExecuting(context);
  }
}

Wrap Up

301 is an HTTP status code sent by a web server to a browser. A 301 signals a permanent redirect from one URL to another, meaning all users that request an old URL will be automatically sent to a new URL.

Post a Comment

Previous Post Next Post