Markus Johansson
03/28/2024, 11:21 AMkdx-perbol
03/28/2024, 1:06 PMUmbracoApplicationUrl
setting, but I'm not sure what it controls...Anders Bjerner
03/28/2024, 2:12 PMcsharp
private static void RewriteHttpHost(RewriteContext context) {
context.Result = RuleResult.ContinueRules;
// Get a reference to the current request
HttpRequest request = context.HttpContext.Request;
// Get the host from the header
string? proxyHost = request.Headers["x-forwarded-host"];
if (!string.IsNullOrWhiteSpace(proxyHost)) return;
// Update the HTTP host
request.Host = new HostString(proxyHost);
}
Markus Johansson
03/28/2024, 3:02 PMAnders Bjerner
03/28/2024, 3:08 PMMarkus Johansson
03/28/2024, 3:09 PMAnders Bjerner
03/28/2024, 3:27 PMConfigure
method in `Startup.cs`:
csharp
app.UseRewriter(new RewriteOptions().Add(RewriteHttpHost));
The Umbraco 13 template doesn't have a Startup.cs
, but it looks somewhat similar in Program.cs
. I can see my coworker added it between await app.BootUmbracoAsync();
and app.UseUmbraco()
.
I don't know if it makes any difference, but probably wouldn't hurt adding it before await app.BootUmbracoAsync();
.
https://cdn.discordapp.com/attachments/1222868066858438737/1222930014157406258/image.png?ex=66180154&is=66058c54&hm=9b2200ac47d37ff80605a97a46b54512ad889b89c9e52473a09f12b73f797a38&Markus Johansson
03/28/2024, 3:38 PMcsharp
public class ProxyHostRewriteRule : Microsoft.AspNetCore.Rewrite.IRule
{
public void ApplyRule(RewriteContext context)
{
context.Result = RuleResult.ContinueRules;
// Get a reference to the current request
HttpRequest request = context.HttpContext.Request;
// Get the host from the header
string? proxyHost = request.Headers["x-forwarded-host"];
if (!string.IsNullOrWhiteSpace(proxyHost))
{
// Update the host
request.Host = new HostString(proxyHost);
}
string? proxyScheme = request.Headers["x-forwarded-scheme"];
if (!string.IsNullOrWhiteSpace(proxyScheme))
{
// Update the scheme (http/https)
request.Scheme = proxyScheme;
}
}
}
and then during startup:
csharp
var rewriteOptions = new RewriteOptions();
rewriteOptions.Rules.Add(new ProxyHostRewriteRule());
app.UseRewriter(rewriteOptions);
Thanks a lot for helping out!