Best practice for http redirection for Windows Azure
Solution 1
You might want to instead use the IIS rewrite module (seems "cleaner"). Here's a blog post that shows how to do this: http://weblogs.asp.net/owscott/archive/2009/11/30/iis-url-rewrite-redirect-multiple-domain-names-to-one.aspx. (You'll just need to put the relevant markup in web.config.)
An example rule you could use is:
<rule name="cloudexchange" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="cloudexchange.cloudapp.net" />
</conditions>
<action type="Redirect" url="http://odata.stackexchange.com/{R:0}" />
</rule>
Solution 2
This is what I did:
We have a base controller class we use for all our controllers, we now override:
protected override void OnActionExecuted(ActionExecutedContext filterContext) {
var host = filterContext.HttpContext.Request.Headers["Host"];
if (host != null && host.StartsWith("cloudexchange.cloudapp.net")) {
filterContext.Result = new RedirectPermanentResult("http://odata.stackexchange.com" + filterContext.HttpContext.Request.RawUrl);
} else
{
base.OnActionExecuted(filterContext);
}
}
And added the following class:
namespace StackExchange.DataExplorer.Helpers
{
public class RedirectPermanentResult : ActionResult {
public RedirectPermanentResult(string url) {
if (String.IsNullOrEmpty(url)) {
throw new ArgumentException("url should not be empty");
}
Url = url;
}
public string Url {
get;
private set;
}
public override void ExecuteResult(ControllerContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
if (context.IsChildAction) {
throw new InvalidOperationException("You can not redirect in child actions");
}
string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext);
context.Controller.TempData.Keep();
context.HttpContext.Response.RedirectPermanent(destinationUrl, false /* endResponse */);
}
}
}
The reasoning is that I want a permanent redirect (not a temporary one) so the search engines correct all the bad links.
Sam Saffron
Co-founder: http://www.discourse.org email: [email protected] blog: http://samsaffron.com twitter: @samsaffron Ex Stack Overflow Valued Associate #00008, creator of Stack Exchange Data Explorer, co-creator of Media Browser ( now Emby ) Other Projects: Logster : https://github.com/discourse/logster rack-mini-profiler : https://github.com/MiniProfiler/rack-mini-profiler message_bus : https://github.com/SamSaffron/message_bus memory_profiler: https://github.com/SamSaffron/memory_profiler flamegraph : https://github.com/SamSaffron/flamegraph All original source snippets I post on Stack Overflow are dedicated to the public domain. Do with them as you see fit.
Updated on June 05, 2022Comments
-
Sam Saffron about 2 years
I have an azure website which is named:
http://myapp.cloudapp.net
Of-course this URL is kind of ugly so I set up a CNAME that points
http://www.myapp.com
to the azure url.All is well up until here, but there is a snag.
http://myapp.cloudapp.net
has leaked out and now is indexed by google and lives on other sites.I would like to permanently redirect any requests for myapp.cloudapp.net to the new home at www.myapp.com
The website I have is coded in MVC.Net 2.0, as this is an azure app, there is not UI to access IIS and everything needs to be done in application code or web.config.
What is a clean way to set a permanent redirect in place, should it go in web.config or in a global controller?
-
user94559 about 14 yearsURL Rewrite 2.0 is installed on all Windows Azure VMs. (2.0 is available in OS 1.3 and above.)
-
Sam Saffron about 14 yearsYerp just found out here: msdn.microsoft.com/en-us/library/dd573358.aspx I'm accepting this answer cause it somewhat cleaner, will not have to litter my code
-
kamranicus about 10 yearsIt's 4 years later; is this still the only way to do this? I am loving Azure but the redirection stuff is kind of annoying; i.e. have to use DNS to redirect www to non-www; have to implement this to redirect *.azurewebsites.net to custom domain. This should be standard stuff that should have a checkbox in the site configuration settings.
-
Nico Westerdale about 10 yearsAdd redirectType="Permanent" to the action tag - Google will like that much better
-
urig about 10 yearsWhat if I want to remove the Azure website altogether, but still have requests to it routed to the "external" name. Can I put a CNAME record from myapp.cloudapp.net to www.myapp.com even if the Azure website is gone?