BitBadger.AspNetCore.CanonicalDomains 1.0.0

dotnet add package BitBadger.AspNetCore.CanonicalDomains --version 1.0.0
NuGet\Install-Package BitBadger.AspNetCore.CanonicalDomains -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BitBadger.AspNetCore.CanonicalDomains" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BitBadger.AspNetCore.CanonicalDomains --version 1.0.0
#r "nuget: BitBadger.AspNetCore.CanonicalDomains, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BitBadger.AspNetCore.CanonicalDomains as a Cake Addin
#addin nuget:?package=BitBadger.AspNetCore.CanonicalDomains&version=1.0.0

// Install BitBadger.AspNetCore.CanonicalDomains as a Cake Tool
#tool nuget:?package=BitBadger.AspNetCore.CanonicalDomains&version=1.0.0

BitBadger.AspNetCore.CanonicalDomains

This package provides ASP.NET Core middleware to enforce canonical domains.

What It Does

Having multiple domain names pointing to the same content can lead to inconsistency and diluted search rankings. This middleware intercepts known alternate domains and redirects requests to the canonical domain, ensuring uniformity and unified search rankings.

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.

How to Use

First, install this package.

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.

Troubleshooting

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.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 276 7/8/2023

Initial release