32

using the following code

context.Response.StatusCode = 301;

context.Response.Redirect(newUrl, true);
context.Response.End();

I can see in fiddler that it's using a 302 instead of a 301. Should I be setting the status after the redirect call?

ILovePaperTowels
  • 1,445
  • 2
  • 17
  • 30
  • 1
    I know this is a really old question, but in the interest of posterity, it should be noted that a redirect code of 301 is usually counterproductive; if you have to change this redirect, including to revert it and host the content at the original location you had redirected from, the site will break for any users that were already redirected, and clearing cached 301 entries can require "power-user" knowledge and access which the average site visitor will not have. A 302 isn't cached, and while that slows loading, it can be changed at will and browsers will respond correctly. – KeithS Dec 27 '17 at 16:53

5 Answers5

54

If you're using ASP.Net 4.0, you can use Response.RedirectPermanent, which will use 301 instead of 302.

Adam Butler
  • 2,733
  • 23
  • 27
37

Response.Redirect() will overwrite the StatusCode property with the code for a redirect (302). Also, because you're using the Response.Redirect() overload taking the boolean parameter, you should set it to False if you want to call Response.End() yourself. Otherwise it's redundant and can cause errors.

Try the following (pre-ASP.NET 4.0; Adam Butler's answer covers the new best practice):

context.Response.Redirect(newUrl, false);
context.Response.StatusCode = 301;
context.Response.End();
KeithS
  • 70,210
  • 21
  • 112
  • 164
4

301 is cache-able. if you are using ASP.NET 4.0 , you can use RedirectPermanent.

Also, set your statuscode after Redirect

also, look into these answers. Response.Redirect HTTP status code

Community
  • 1
  • 1
Priyank
  • 10,503
  • 2
  • 27
  • 25
0

I am combining the answers above with something I use if I have old domains/sub-domains for different versions of a site that I want to redirect to the current, mostly for SEO reasons, so as to not have multiple versions of the same site at different URLs:

using System;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace myapp.web {
  public class Global : HttpApplication {
    void Application_Start(object sender, EventArgs e) {
      // Code that runs on application startup
      AreaRegistration.RegisterAllAreas();
      GlobalConfiguration.Configure(WebApiConfig.Register);
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
      RouteConfig.RegisterRoutes(RouteTable.Routes);
      BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

    protected void Application_BeginRequest(object sender, EventArgs e) {
      //some of these checks may be overkill
      if ((HttpContext.Current != null)
        && (HttpContext.Current.Request != null)
        && (HttpContext.Current.Request.ServerVariables != null)
        && (!String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_HOST"]))
        ) {
        switch (HttpContext.Current.Request.ServerVariables["HTTP_HOST"]) {
          case "old.url.com":
            HttpContext.Current.Response.RedirectPermanent("https://new.url.com", true);
            //status code is not needed if redirect perm is used
            HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
            HttpContext.Current.Response.End();
            break;
          case "nightly.old.url.com":
            HttpContext.Current.Response.RedirectPermanent("https://nightly.new.url.com", true);
            //status code is not needed if redirect perm is used
            HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
            HttpContext.Current.Response.End();
            break;
        }
      }
    }
  }
}
sobelito
  • 1,525
  • 17
  • 13
0

For me that works

Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "NewURL"
Response.end