How do I use IIS7 rewrite to redirect requests for (HTTP or HTTPS):// (www or no-www) .domainaliases.ext to HTTPS://maindomain.ext

Posted by costax on Pro Webmasters See other posts from Pro Webmasters or by costax
Published on 2012-09-29T04:15:18Z Indexed on 2012/09/29 9:49 UTC
Read the original article Hit count: 262

Filed under:
|
|

I have multiple domain names assigned to the same site and I want all possible access combinations redirected to one domain. In other words whether the visitor uses http://domainalias.ext or http://www.domainalias.ext or https://www.domainalias3.ext or https://domainalias4.ext or any other combination, including http://maindomain.ext, http://www.maindomain.ext, and https://www.maindomain.ext they are all redirected to https://maindomain.ext

I currently use the following code to partially achieve my objectives:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^MAINDOMAIN\.EXT$"     negate="true" />
                    </conditions>
                    <action type="Redirect" redirectType="Permanent" url="https://MAINDOMAIN.EXT/{R:1}" />
                </rule>
                <rule name="HTTP2HTTPS" stopProcessing="true">
                  <match url="(.*)" />
                    <conditions>
                      <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                  <action type="Redirect" redirectType="Permanent" url="https://MAINDOMAIN.EXT/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

...but it fails to work in all instances. It does not redirect to https://maindomain.ext when user inputs https://(www.)domainalias.ext

So my question is, are there any programmers here familiar with IIS7 ReWrite that can help me modify my existing code to cover all possibilities and reroute all my domain aliases, loaded by themselves or using www in front, in HTTP or HTTPS mode, to my main domain in HTTPS format???

The logic would be: if entire URL does NOT start with https://maindomain.ext then REDIRECT to https://maindomain.ext/(plus_whatever_else_that_followed).

Thank you very much for your attention and any help would be appreciated.

NOTE TO MODS: If my question is not in the correct format, please edit or advise. Thanks in advance.

© Pro Webmasters or respective owner

Related posts about redirects

Related posts about url-rewriting