subdomaindotnetnukeiis-10url-rewrite-modulecanonicalization

IIS10 UrlRewrite, multiple canonical TLD's & non-canonical sub-domains + HTTPS


I'm having some trouble with URL rewrite.

I have a website that hosts multiple child sites all within the same Site in IIS10. The primary site has 3 TLD's that should redirect to a primary canonical TLD.

On the same site there are multiple additional bindings for sub-domains of the primary TLD that I don't want redirecting to my primary TLD.

This is how I'd like to have URL rewrite working:

TLD's

mydomain.com

mydomain2.com

mydomain3.com

--> mydomain2.com & mydomain3.com should redirect to mydomain.com with HTTP to HTTPS.

Sub Domains

c1.mydomain.com

c2.mydomain.com

c3.mydomain.com

etc..

--> c1.mydomain.com, c2.mydomain.com, c3.mydomain.com, etc... should NOT redirect to mydomain.com with HTTP to HTTPS.

These are my problematic areas

If I create a redirect HTTP to HTTPS rule then mydomain2.com & mydomain3.com don't pull up as my SSL cert is only for mydomain.com and I don't want mydomain2.com & mydomain3.com as the site's URL, I only want them to redirect to mydomain.com

If I don't create a canonical rule then when visiting either mydomain2.com or mydomain3.com it will fail as there is no matching SSL for those TLD's and the HTTP to HTTPS rule is sending visitors to the HTTPS version a TLD that has not HTTPS binding. Additionally I don't want those URL's staying in the address bar, I want the final domain to be mydomain.com

If I DO create a canonical rule then all my sub-domain bindings will now redirect to mydomain.com which is problematic as those are completely different websites being redirected.

I'm using DNN as the CMS so if your familiar with Portals, that's what's happening all child portals are now redirecting to my parent portal and I don't want that.


UPDATE per Lex Li's request

Rules 1) Here is the URL Rewrite rule for redirecting HTTP to HTTPS

The problem here is that I don't have a canonical redirect to my primary domain.

<rewrite>
            <rules>
                <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
                </rule>
            </rules>
        </rewrite>

Rules 2) Here is the URL rewrite rules WITH canonical redirect + HTTP to HTTPS

The problem here is that subdomain.mysite.com is redirecting to mysite.com where it shouldn't. I see there is a condition's area perhaps there is a way to condition this rule so it doesn't apply to my subdomains?

<rewrite>
            <rules>
                <rule name="CanonicalHostNameRule1">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://example.com/{R:1}" />
                </rule>
                <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
                </rule>
            </rules>
        </rewrite>

Observations:

Rules 1 Enabled:

example.com redirects to https://example.com (correct)

example2.com redirects to https://example2.com (fails | no SSL on example2.com & it should redirect to example.com)

sub.example.com redirects to https://sub.example.com (correct)

Rules 2 Enabled:

example.com redirects to https://example.com (correct)

example2.com redirects to https://example.com (correct)

sub.example.com redirects to https://example.com (fails, it should redirect to https://sub.example.com)


Solution

  • Modify rule "CanonicalHostNameRule1" to,

                    <rule name="CanonicalHostNameRule1">
                        <match url="(.*)" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
                            <add input="{HTTP_HOST}" pattern="^sub\.example\.com$" negate="true" />
                        </conditions>
                        <action type="Redirect" url="http://example.com/{R:1}" />
                    </rule>