简体   繁体   English

301使用负载均衡器Azure C#重定向

[英]301 redirect with load balancer azure c#

I am trying to do 301 redirect with following code: 我正在尝试使用以下代码进行301重定向:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    var url = HttpContext.Current.Request.Url.ToString().ToLower();
    var contentId = GetIdFromUrl(url);

    var forwardedHost = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_HOST"];
    var proxiedBrand = HttpContext.Current.Request.Headers["HOST"].ToLower();
    var isRedirectionTrue = IsRedirectionArticleInCollection(contentId);
    var isForwardedHostTrue = forwardedHost.Contains("old.com");
    var urlRedir = url.Replace("v3", "www").Replace("old.com", "new.com");

    Logger.Warn("urlredir: " + urlRedir + ", ContentId: " + contentId + ", Url:" + url + ", isRedirectionTrue: " + isRedirectionTrue + ", ForwardedHost:" + forwardedHost + ", Host: " + proxiedBrand + ", IsForwardedHostTrue: " + isForwardedHostTrue);
    if (contentId > 0 && isRedirectionTrue && isForwardedHostTrue)
    {
        var urlToRedirectTo = "https://dev.www.new.com/";// url.Replace("v3", "www").Replace("old.com", "new.com");
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.StatusCode = 301;
        HttpContext.Current.Response.AddHeader("Location", urlToRedirectTo);
        HttpContext.Current.Response.Redirect(urlToRedirectTo, false);
        HttpContext.Current.Response.End();
    }
}

Problem is that redirect is not working. 问题是重定向不起作用。 I never get redirected to new domain, whatever i do. 无论我做什么,我都不会重定向到新域。 If I use hardcoded example dev.www.new.com it goes to dev.www.old.com domain, same thing happens with full url. 如果我使用硬编码示例dev.www.new.com,它将进入dev.www.old.com域,则使用完整网址也会发生相同的情况。

Is my redirect correct? 我的重定向正确吗?

If I use hardcoded example dev.www.new.com it goes to dev.www.old.com domain, same thing happens with full url. 如果我使用硬编码示例dev.www.new.com,它将进入dev.www.old.com域,则使用完整网址也会发生相同的情况。

With your code in if judge sentences, it could correctly to go to dev.www.new.com with full url. 如果您的代码中有判断语句,则可以正确地访问带有完整URL的dev.www.new.com。 Check the logger and set another log in if judge sentence to ensure you have run into if sentences. 检查记录器,并设置另一个判断语句是否句子,以确保您遇到了if语句。

Certainly, you could also use url rewrite to redirect based on the logic expressed in the rewrite rules . 当然,您还可以根据重写规则中表达逻辑使用url rewrite进行重定向。 For more details, you could refer to this link . 有关更多详细信息,您可以参考此链接

If you still have any problem, please show me detailed your current domain and the redirected domain you want. 如果仍然有任何问题,请告诉我详细的当前域和所需的重定向域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM