Last active
August 29, 2015 14:02
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The goal with this approach was to make it easy to feed the primaryUrl from Umbraco content ...