【问题标题】:Best practice for http redirection for Windows AzureWindows Azure 的 http 重定向最佳实践
【发布时间】:2010-06-21 02:26:48
【问题描述】:

我有一个名为:

  • http://myapp.cloudapp.net

当然,这个 URL 有点丑,所以我 set up a CNAMEhttp://www.myapp.com 指向天蓝色的 URL。

到这里为止一切都很好,但是有一个障碍。

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

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

我的网站是用 MVC.Net 2.0 编码的,因为这是一个天蓝色的应用程序,没有 UI 可以访问 IIS,一切都需要在应用程序代码或 web.config 中完成。

什么是设置永久重定向的干净方法,应该在 web.config 中还是在全局控制器中?

【问题讨论】:

    标签: c# iis asp.net-mvc-2 web-config azure


    【解决方案1】:

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

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

        <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>
    

    【讨论】:

    • URL Rewrite 2.0 安装在所有 Windows Azure VM 上。 (2.0 在 OS 1.3 及更高版本中可用。)
    • Yerp 刚刚在这里发现:msdn.microsoft.com/en-us/library/dd573358.aspx 我接受这个答案,因为它有点干净,不必乱扔我的代码
    • 4年后;这仍然是唯一的方法吗?我喜欢 Azure,但重定向的东西有点烦人;即必须使用 DNS 将 www 重定向到非 www;必须实现此以将 *.azurewebsites.net 重定向到自定义域。这应该是标准的东西,应该在站点配置设置中有一个复选框。
    • 将 redirectType="Permanent" 添加到操作标签 - Google 会更喜欢这样
    • 如果我想完全删除 Azure 网站,但仍有对它的请求路由到“外部”名称,该怎么办。即使 Azure 网站不见了,我可以将 CNAME 记录从 myapp.cloudapp.net 放到 www.myapp.com 吗?
    【解决方案2】:

    这就是我所做的:

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

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

    原因是我想要一个永久重定向(不是临时重定向),以便搜索引擎更正所有错误链接。

    【讨论】:

      猜你喜欢
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多