Skip to content

Instantly share code, notes, and snippets.

@jesperordrup
Last active August 29, 2015 14:02
Show Gist options
  • Save jesperordrup/64d5d3f83fcbc2ac725b to your computer and use it in GitHub Desktop.
Save jesperordrup/64d5d3f83fcbc2ac725b to your computer and use it in GitHub Desktop.
redirect to Primary - if page is requested via any other url than primaryUrl then permanent redirect to primaryUrl
@{
Layout = null;
string primaryUrl = "www.jesper.com";
string adjustedUrl = "";
bool doRedirect = false;
var thisUrl = HttpContext.Current.Request.Url;
string host = thisUrl.Host.ToLower();
string PathAndQuery = thisUrl.PathAndQuery;
string scheme = thisUrl.Scheme;
int port = thisUrl.Port;
if (host != primaryUrl)
{
string sPort = port == 80 ? "" : ":" + port;
adjustedUrl = scheme + "://" + primaryUrl + sPort + PathAndQuery;
doRedirect = true;
}
if (doRedirect && !host.Contains("localhost"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", adjustedUrl);
}
}
@jesperordrup
Copy link
Author

The goal with this approach was to make it easy to feed the primaryUrl from Umbraco content ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment