301 Permanent Redirect in ASP.NET MVC

What is a 301 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 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 Redirect?
The main reasons to set up a 301 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 in your Base Controller as well:

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);
}
}

YOU MAY ALSO LIKE...

Zender helps you automate marketing emails throughout the customer journey using a powerful API. Create and Send html based emails, Sign Up Confirmation Emails, Change Password Emails, Campaigns, Advertising and Media News Emails

Check out the API http://zender.sharptag.com and let us know how can we support you.

1 Comment

Add a Comment

As it will appear on the website

Not displayed

Your website