w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Complex redirect: HTTP to HTTPS + port on localhost and naked domain to www, except on localhost

I made a ASP.NET/C# solution. Basically:

private void Application_BeginRequest()
{
    var canonicalAuthority =
WebConfigurationManager.AppSettings.Get("CanonicalAuthority")
                                 ?? this.Request.Url.Authority;
    // TODO: Might not work with International Domain Names.
    if (!Request.IsSecureConnection
        || !this.Request.Url.Authority.Equals(canonicalAuthority,
StringComparison.OrdinalIgnoreCase))
    {
        string path = "https://" + canonicalAuthority +
this.Request.Url.PathAndQuery;
        this.Response.RedirectPermanent(path, false);
        this.CompleteRequest();
    }
}

Note the TODO: I think there might be problems with URL encoding of international domain names, but it could probably be fixed by using Uri.Compare.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.