Skip to content

Instantly share code, notes, and snippets.

@rrreese
Created November 17, 2012 09:30
Show Gist options
  • Save rrreese/4094476 to your computer and use it in GitHub Desktop.
Save rrreese/4094476 to your computer and use it in GitHub Desktop.
301 Canonical Redirects in ASP.net MVC
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Request.Url.Authority.StartsWith("www"))
return;
string url = (Request.Url.Scheme
+ "://www."
+ HttpContext.Current.Request.Url.Authority
+ HttpContext.Current.Request.Url.AbsolutePath
);
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", url);
Response.End();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment