View Source Repo

The Short Version

If multiple domains point to the same website, ensuring that links are served on a specific one can benefit both users and site maintainers. This middleware provides an application-focused way to enforce these canonical domains.

How to Use

First, install BitBadger.AspNetCore.CanonicalDomains from NuGet. Nuget

Second, add the configuration for each domain that needs to be redirected; this middleware will configure itself via the details in the CanonicalDomains configuration key. An example:

    "CanonicalDomains": [
            "From": "www.example.com",
            "To": "example.com"
            "From": "web.example.com",
            "To": "example.com"

Finally, in your main source file (Program.cs, App.fs, etc.), import the namespace BitBadger.AspNetCore.CanonicalDomains, and call .UseCanonicalDomains() on the IApplicationBuilder instance. It should be placed after .UseForwardedHeaders(), if that is used, but should be ahead of .UseStaticFiles(), auth config, endpoints, etc. It should be run as close to the start of the pipeline as possible, as no other processing should take place until the request is made on the canonical domain.


If the ASP.NET Core application is running behind a reverse proxy (Nginx, Apache, IIS, etc.), enforcing these domains at that point is the most efficient. This middleware is designed for scenarios where the application is being served directly or in a container, with multiple domains pointed at the running application.


This middleware will not throw errors if it cannot parse its configuration properly (feel free to do the final step before adding configuration to verify!). However, if .UseCanonicalDomains() is called, and the setup does not find anything to do, it will emit a warning in the log, and will not add the middleware to the pipeline. If redirection is not occurring as you suspect it should, check the top of the log when the application starts.


Questions and suggestions can be sent via issues on the project's repository.