Windows Azure的http重定向的最佳实践

我有一个名为azure的azure色网站:

  • http://myapp.cloudapp.net

当然这个URL有点难看,所以我设置了一个将http://www.myapp.com指向azure url 的CNAME 。

一切都很好,直到这里,但有一个障碍。

http://myapp.cloudapp.net已泄露出来,现在被谷歌索引并存在于其他网站上。

我想将myapp.cloudapp.net的任何请求永久重定向到www.myapp.com的新家

我的网站是用MVC.Net 2.0编写的,因为这是一个azure色的应用程序,没有用于访问IIS的UI,所有内容都需要在应用程序代码或web.config中完成。

什么是设置永久重定向的简洁方法,如果它进入web.config或全局控制器?

您可能希望改为使用IIS重写模块(看起来“更干净”)。 这是一篇博客文章,展示了如何执行此操作: http : //weblogs.asp.net/owscott/archive/2009/11/30/iis-url-rewrite-redirect-multiple-domain-names-to-one.aspx 。 (您只需要将相关标记放在web.config中。)

您可以使用的示例规则是:

         

这就是我做的:

我们有一个我们用于所有控制器的基本控制器类,我们现在覆盖:

  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); } } 

并添加了以下类:

 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 */); } } } 

原因是我想要永久重定向(而不是临时重定向),以便搜索引擎纠正所有不良链接。