Search code examples
asp.net-coreiis

Minimum ASP.NET Core app to redirect to new domain


Which is the most efficient and reliable way to redirect all requests to an old domain on IIS to the home page of a new domain on the same server? Our company owns the server, so we can do what we like with it.

One option that pops up is:

using Microsoft.AspNetCore.Rewrite;

WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args);
WebApplication app = builder.Build();

RewriteOptions rewriteOptions = new RewriteOptions();
rewriteOptions.AddRedirect("https://www.olddomain.com*", "https://www.newdomain.com", statusCode: StatusCodes.Status301MovedPermanently);

app.UseRewriter(rewriteOptions);
app.Run();

Is this going to redirect all requests as I hope, or is there a more efficient and reliable solution?

Is this better done using IIS rules?

<system.webServer>
    <rewrite>
        <rules>
            <rule name="oldDomain.comRedirect" stopProcessing="true">
                <match url="(.*)" />
                <action type="Redirect" url="https://www.newDomain.com" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

Solution

  • This is very simple in IIS, you can use URL Rewrite if you want to but it's not necessary.

    From within IIS Manager:

    1. Select your site
    2. Open the "HTTP Redirect" feature
    3. Enable the "Redirect requests to this destination" checkbox and fill in the URL of the new site.
    4. Enable the other options as necessary for your use case.
    5. Set the desired Status Code. Only use "Permanent (301)" if you know for sure that your old domain will not be used in the future.

    You can also disable the old site, add the old domain to the new website, and then use URL Rewrite.