using System; using Microsoft.AspNetCore.Rewrite; namespace StardewModdingAPI.Web.Framework { /// Rewrite requests to prepend the subdomain portion (if any) to the path. /// Derived from . public class RewriteSubdomainRule : IRule { /// Applies the rule. Implementations of ApplyRule should set the value for (defaults to RuleResult.ContinueRules). /// The rewrite context. public void ApplyRule(RewriteContext context) { context.Result = RuleResult.ContinueRules; // get host parts string host = context.HttpContext.Request.Host.Host; string[] parts = host.Split('.'); // validate if (parts.Length < 2) return; if (parts.Length < 3 && !"localhost".Equals(parts[1], StringComparison.InvariantCultureIgnoreCase)) return; // prepend to path context.HttpContext.Request.Path = $"/{parts[0]}{context.HttpContext.Request.Path}"; } } }