最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Redirect base url Azure websites to custom domain - Stack Overflow

programmeradmin5浏览0评论

We have on promise application hosted in Azure website that use on-premise custom domain. It works most of the time with the custom domain, but when session expires, it redirect to Azure website.

Since the application is dockerized and hosted on Linux web app, the web.config rewrite rules do not work for me.

I tried using a .NET Core middleware, but the regex handle only relative path, so could not check if base url contain azurewebsite and thus I can not redirect in this case.

var rewrite = new RewriteOptions()
                .AddRedirect("the regex here check only relative path !!", "custom.domain");
app.UseRewriter(rewrite);

I tried this one also on startup throw "too many redirect" error

app.Use(async (context, next) =>
{
    var url = context.Request.Host.Value;

    if (url.Contains("azurewebsites"))
    {
            context.Response.Redirect(";);
            return;
    }

    await next();
});

Any help on this topic would be much appreciated.

Thank you.

We have on promise application hosted in Azure website that use on-premise custom domain. It works most of the time with the custom domain, but when session expires, it redirect to Azure website.

Since the application is dockerized and hosted on Linux web app, the web.config rewrite rules do not work for me.

I tried using a .NET Core middleware, but the regex handle only relative path, so could not check if base url contain azurewebsite and thus I can not redirect in this case.

var rewrite = new RewriteOptions()
                .AddRedirect("the regex here check only relative path !!", "custom.domain");
app.UseRewriter(rewrite);

I tried this one also on startup throw "too many redirect" error

app.Use(async (context, next) =>
{
    var url = context.Request.Host.Value;

    if (url.Contains("azurewebsites"))
    {
            context.Response.Redirect("https://custom.domain");
            return;
    }

    await next();
});

Any help on this topic would be much appreciated.

Thank you.

Share edited Mar 7 at 12:24 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 7 at 11:27 moyomehmoyomeh 1073 silver badges16 bronze badges 3
  • Just clarify: When you say "We have on promise application hosted in Azure website that use on-premise custom domain", does that mean that the app is hosted in xxx.azurewebsites and you have a domain name customdomain? – pumpkin Commented Mar 11 at 14:33
  • If that is the case and you have access to your domain registrar, then just add the AAAA or A entries in your DNS table, I guess that should solve it if that is the case – pumpkin Commented Mar 11 at 14:35
  • No it's private domain, we have in house dns that link the ip adress of the load balancer to the custom domain name, – moyomeh Commented Mar 11 at 16:02
Add a comment  | 

1 Answer 1

Reset to default 1 +50

Application gateway inserts six additional headers to all requests before it forwards the requests to the backend. These headers are x-forwarded-for, x-forwarded-port, x-forwarded-proto, x-original-host, x-original-url, and x-appgw-trace-id. X-original-host header contains the original host header with which the request arrived. This header is useful in Azure website integration, where the incoming host header is modified before traffic is routed to the backend. If session affinity is enabled as an option, then it adds a gateway-managed affinity cookie. For more info, please see this link: https://learn.microsoft/en-us/azure/application-gateway/how-application-gateway-works#modifications-to-the-request

The above is according to the Microsoft's Azure Application Gateway webpage. You can capture the X-Original-Host header and redirect to it in your Startup.cs; something like this:

app.Use(async (context, next) =>
{
  if (context.Request.Headers.GetValues("X-Original-Host") != null)
  {
    var originalHost = context.Request.Headers.GetValues("X-Original-Host").FirstOrDefault();
    context.Request.Headers.Set("Host", originalHost);
  }
  await next.Invoke();
});
发布评论

评论列表(0)

  1. 暂无评论