When I deploy my website as a Azure Web App, my AddRedirectToWwwPermanent() rewrite rule is adding "www." to the front of the default azure URL, e.g. www.myproject.azurewebsites.net. Using "www" with the subdomain, causes the page not to load.
Is there a way to skip the AddRedirectToWwwPermanent() rewrite rule specifically for the *.azurewebsites.net URL?
I'm currently using the Rewriter in Startup.cs:
app.UseRewriter(new RewriteOptions()
.AddRedirectToWwwPermanent()
);
Alternatively, is there a way to redirect the *.azurewebsites.net URL to my own custom domain? I've tried the following, but it doesn't work:
app.UseRewriter(new RewriteOptions()
.AddRedirect(@"myproject.azurewebsites.net", @"www.example.com", StatusCodes.Status301MovedPermanently)
.AddRedirectToWwwPermanent()
);
Update, also tried:
app.UseRewriter(new RewriteOptions()
.AddRedirect(@"https://myproject.azurewebsites.net", @"https://www.example.com", StatusCodes.Status301MovedPermanently)
.AddRedirectToWwwPermanent()
);
Here's my solution, which works in ASP.NET Core 2.1. However, I don't know if this is the most efficient way to do the redirect, so I'm open to better suggestions.
Since I couldn't get the built in AddRedirect() rule to work (I still think I may have just been using it wrong), I created a custom rewrite rule to do the redirect.
Here's my RedirectAzureWebsitesRule, which redirects *.azurewebsites.net to the custom domain:
public class RedirectAzureWebsitesRule : IRule
{
public int StatusCode { get; } = (int)HttpStatusCode.MovedPermanently;
public void ApplyRule(RewriteContext context)
{
HttpRequest request = context.HttpContext.Request;
HostString host = context.HttpContext.Request.Host;
if (host.HasValue && host.Value == "my-project.azurewebsites.net")
{
HttpResponse response = context.HttpContext.Response;
response.StatusCode = StatusCode;
response.Headers[HeaderNames.Location] = request.Scheme + "://" + "www.example.com" + request.PathBase + request.Path + request.QueryString;
context.Result = RuleResult.EndResponse;
}
else
{
context.Result = RuleResult.ContinueRules;
}
}
}
Then in Startup.cs, I called the new rule, before AddRedirectToWwwPermanent():
app.UseRewriter(new RewriteOptions()
.Add(new RedirectAzureWebsitesRule())
.AddRedirectToWwwPermanent()
);
Just as a slight variation to Wellspring's answer. If you want to keep the azurewebsites.net domain running still you could do the following:
public class RedirectAzureWebsitesRule : IRule
{
public void ApplyRule(RewriteContext context)
{
HttpRequest request = context.HttpContext.Request;
HostString host = context.HttpContext.Request.Host;
if (host.HasValue && host.Value.ToLower().Contains(".azurewebsites.net"))
{
//if we are viewing on azure's domain - skip the www redirect
context.Result = RuleResult.SkipRemainingRules;
}
else
{
context.Result = RuleResult.ContinueRules;
}
}
}
This will ensure you can run on both domains (if you have a need to):
app.UseRewriter(new RewriteOptions()
.Add(new RedirectAzureWebsitesRule())
.AddRedirectToWwwPermanent()
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With