How to redirect azurewebsites.net domain when host...
# help-with-umbraco
t
Setting up a .NET web app on Azure nets you an automatic `projectname.azurewebsites.net`domain. But you might also opt for setting up a custom domain. Doing so you probably want all your traffic to go to the custom domain, effectively redirecting all requests to the automatically assigned azurewebsites domain to your custom domain. How would you go about setting this up? Years ago one would use the web.config file. Is this possible to use in modern .NET Core contexts? My limited knowledge on this says that web.config is for IIS servers, while newer solutions - inlucing Umbraco - use the Kestrel server.
m
You don't need to set up redirects. You need to add your custom domain to the webapp and configure the records of your DNS. When you try to add new domains to the webapp, you get a popup with some instructions of how to do it.
t
@Mario Lopez Yes, these parts are all set up correctly. But this does not handle redirects from
projectname.azurewebsites.net
to
yourcustomdomain.com
Sure, we can hope that nobody visits the auto-assigned url's, but there's the risk of it leaking somehow and then Google might try indexing it and so on.
This is in general a recommended approach, to not serve your content over multiple domains, as it can negatively impact SEO etc.
m
ah, got it.
then you need to use a middleware. Something like this should work:
Copy code
public class RedirectionMiddleware
{
    private readonly RequestDelegate _next;
    public RedirectionMiddleware(RequestDelegate next)
    {
        _next = next;
    }
    public async Task InvokeAsync(HttpContext httpContext)
    {

        // Redirect to login if user is not authenticated. This instruction is neccessary for JS async calls, otherwise everycall will return unauthorized without explaining why
        if (httpContext.Request.Host.Value == "domain.azurewebsites.com")
        {
            httpContext.Response.Redirect("https://yourdomain.com");
        }

        // Move forward into the pipeline
        await _next(httpContext);
    }
}
public static class RedirectionMiddlewareExtensions
{
    public static IApplicationBuilder UseRedirectionMiddleware(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<RedirectionMiddleware>();
    }
}
You can use it in your
Program.cs
before Umbraco is called.
Copy code
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
    .AddDeliveryApi()
    .AddComposers()
    .Build();

WebApplication app = builder.Build();

await app.BootUmbracoAsync();

app.UseHttpsRedirection();

app.UseRedirectionMiddleware(); // <-- use it here

app.UseUmbraco()
    .WithMiddleware(u =>
    {
        u.UseBackOffice();
        u.UseWebsite();
    })
    .WithEndpoints(u =>
    {
        u.UseBackOfficeEndpoints();
        u.UseWebsiteEndpoints();
    });

await app.RunAsync();
d
What Mario says works, but web.config is still a viable approach as well and according to Microsoft preferred over aspdotnet middlewares. We still use web.config in our dotnet 6 projects hosted in azure and it works fine
That is: if your appservice runs in windows
t
Ah, we're running on Linux currently. Does windows hosted app run IIS in front of Kestrel maybe? Or simply 100% IIS, no Kestrel? For this specific scenario (block/prevent indexing on wrong domain), we can get away with simply blocking any requests there and I found a simple way to do that. In appsettings I can set
AllowedHosts
to the host we want to run on, then all other domains will only render a bad request response when reaching the server.
d
> Does windows hosted app run IIS in front of Kestrel maybe? It used to do exactly that. Perhaps @User can help as he has helped me with similar using Cloudflare
c
asp.net has built in rewriting bits..
I would just do this:
app.UseRewriter(new RewriteOptions().AddRedirect("foo.azurewebsites.net(.*)$", "https://mydomain.com/$1", (int)HttpStatusCode.PermanentRedirect));
98 Views