'.Net 6 Blazor app UseHttpsRedirection is causing many redirects

I have a .NET 6 Blazor Server and another .NET 6 Blazor Wasm site. Both work fine locally but when hosted on a shared hosting site get an error 'too many redirects'. I can't change any setup on the shared hosting so am restricted to web.config & source startup code.

After investigation, the startup code in program.cs

app.UseHttpsRedirection();

is causing the issue. If I comment that out the application works, but if you use an http address it doesn't get redirected to https - so no good. This all happened after I had to set web.config to use out of process hosting model (required by shared hosting when using multiple apps).

<aspNetCore processPath="dotnet" 
    hostingModel="outofprocess" ...

This question was very helpful in discovering the issue. The same site produces "too many redirects" only via cellular, not via WiFi. I also tried forwarding headers as in https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-6.0, but nothings helps.

I'm out of things to try. Any suggestions would be appreciated.



Solution 1:[1]

I had the same issue. Turned out IONOS (1and1 / 1und1) is using Apache as proxy (I have Windows hosting). So you can use a .htaccess file to redirect to https, like:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [L,R=301]

BUT this is only half the way. Websockets do also not work because of the proxy and I still haven't found a solution for that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sven