Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Forked from waldyrfelix/regex-domain-to-www-domain.cs
Last active December 15, 2015 15:08
Show Gist options
  • Save AlbertoMonteiro/5279090 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/5279090 to your computer and use it in GitHub Desktop.
var pattern = @"^(https?://)(?!www[.])(.+)$";
var url = Regex.Replace(Request.Url.AbsoluteUri, pattern, @"$1www.$2");
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", url);
Response.End();
var pattern = @"^(https?://)(?!www[.])(.+)$";
return new SeeOther(Regex.Replace(Request.Url.AbsoluteUri, pattern, @"$1www.$2"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment