'DotNetCore 1.0 MVC how to automatically redirect to a single domain in live
I have multiple domains for a website:
In production, I want the primary domain to be http://www.example.com and for all other associated domains to be automatically redirected to the primary.
Historically I would've done this with URLRewrite, however I'm led to believe that doesn't exist in DotNetCore.
So... how would I do this?
Also, I don't want this to affect development environments.
Solution 1:[1]
An answer that works with DotNetCore 1.0 (also forces https)
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Other configuration code here...
if (env.IsProduction())
{
app.Use(async (context, next) =>
{
if (context.Request.Host != new HostString("www.example.com"))
{
var withDomain = "https://www.example.com" + context.Request.Path;
context.Response.Redirect(withDomain);
}
else if (!context.Request.IsHttps)
{
var withHttps = "https://" + context.Request.Host + context.Request.Path;
context.Response.Redirect(withHttps);
}
else
{
await next();
}
});
}
}
Solution 2:[2]
In .net core 6:
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
var options = new RewriteOptions()
.Add(RewriteRules.ReDirectRequestsToOneHost);
app.UseRewriter(options);
}
public class RewriteRules
{
public static void ReDirectRequestsToOneHost(RewriteContext context)
{
var request = context.HttpContext.Request;
if (request.Host != new HostString("www.example.com"))
{
var redirect = $"{request.Scheme}://www.example.com{request.Path}";
context.HttpContext.Response.Redirect(redirect);
}
}
}
Solution 3:[3]
You can use URLRewrite in .NET Core - just download ASP.NET Core 1.1 Preview 1. And then you will be able to do:
public class Startup {
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
var options = new RewriteOptions()
.AddRedirect("(.*)/$", "$1") // Redirect using a regular expression
.AddRewrite(@"app/(\d+)", "app?id=$1", skipRemainingRules: false) // Rewrite based on a Regular expression
.AddRedirectToHttps(302, 5001) // Redirect to a different port and use HTTPS
.AddIISUrlRewrite(env.ContentRootFileProvider, "UrlRewrite.xml") // Use IIS UrlRewriter rules to configure
.AddApacheModRewrite(env.ContentRootFileProvider, "Rewrite.txt"); // Use Apache mod_rewrite rules to configure
app.UseRewriter(options);
}
}
To update an existing project to ASP.NET Core 1.1 Preview 1 you will need to do the following:
- Download and install the updated .NET Core 1.1 Prevew 1 SDK
- Follow the instructions on the .NET Core 1.1 Preview 1 announcement to update your project to use .NET Core 1.1 Preview 1
- Update your ASP.NET Core packages dependencies to use the new 1.1.0-preview1 versions
More info here: https://blogs.msdn.microsoft.com/webdev/2016/10/25/announcing-asp-net-core-1-1-preview-1/
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 | James Law |
Solution 2 | Tod |
Solution 3 | Dawid Rutkowski |