Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
If we are talking about asp.net core, then you can use middleware. Something like this:
public class RedirectToNoWwwRule : IRule
{
public virtual void ApplyRule(RewriteContext context)
{
var req = context.HttpContext.Request;
if (req.Host.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase))
{
context.Result = RuleResult.ContinueRules;
return;
}
if (!req.Host.Value.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
{
context.Result = RuleResult.ContinueRules;
return;
}
var NoWwwHost = new HostString(req.Host.Value.Substring(4));
var newUrl = UriHelper.BuildAbsolute(req.Scheme, NoWwwHost , req.PathBase, req.Path, req.QueryString);
var response = context.HttpContext.Response;
response.StatusCode = 301;
response.Headers[HeaderNames.Location] = newUrl;
context.Result = RuleResult.EndResponse;
}
}
public static class RewriteOptionsExtensions
{
public static RewriteOptions AddRedirectNoWww(this RewriteOptions options)
{
options.Rules.Add(new RedirectToNoWwwRule());
return options;
}
}
var options = new RewriteOptions();
options.AddRedirectNoWww();
app.UseRewriter(options);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question