iusmar
iusmar

Reputation: 359

IIS Rewrite rules for languages and new domains

I need to create some rules (I thought that 2 would suffice) for the following scenarios:

A. First scenario

https://www.mydomainOLD.com/en/lorem/ipsum/doloret/etc to https://www.mydomainNEW.com/lorem/ipsum/doloret/etc

Basically, I need to change the domain from mydomainOLD to mydomainNEW and remove "/en/" from URL when it appears.

The rule I created is the following:

<rule name="Replace OLD with NEW and remove /en/" enabled="true" stopProcessing="true">
    <match url="^(http|https)://?(www.)mydomainOLD.com/en?(.*)" />
    <conditions logicalGrouping="MatchAny" trackAllCaptures="false" />
    <action type="Redirect" url="https://www.mydomainNEW.com/{R:3}" />
</rule>

B. Second scenario:

https://es.mydomainOLD.com/en/lorem/ipsum/doloret/etc to https://es.mydomainNEW.com/en/lorem/ipsum/doloret/etc

In this case, instead of "es" I can have other countries as well (fr, de, hk, etc.)

The rule I created is the following (for each country):

 <rule name="ES redirect from OLD to NEW" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="^(.*)es.mydomainOLD(.*)$" />
                    </conditions>
                    <action type="Redirect" url="{C:1}be.mydomainNEW.com{C:2}" appendQueryString="false" />
                </rule>

None of these rules are not working as I wanted, even if when I test the regex they work as expected. I think there's something more on the Condition Input that I'm missing. A bit of help would be more than welcome. Thanks.

Upvotes: 0

Views: 212

Answers (2)

Jokies Ding
Jokies Ding

Reputation: 3494

Please try this rule for /en/

 <rule name="rewrite rule" stopProcessing="true">
                <match url="(.*)" />
                <conditions trackAllCaptures="true">
                    <add input="{URL}" pattern="^/en/(.*)" />
                    <add input="{HTTP_HOST}" pattern="(www.)?mydomainOLD.com" />
                </conditions>
                <action type="Redirect" url="https://www.mydomainNEW.com/{C:1}" redirectType="Temporary" />
            </rule>

For somelike old/en/ to new/en/ or old/fr to old/fr, please apply this one.

   <rule name="rewrite rule" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions trackAllCaptures="true">
                        <add input="{URL}" pattern="^/([a-z]{2}/.*)" />
                        <add input="{HTTP_HOST}" pattern="es.mydomainOLD.com" />
                    </conditions>
                    <action type="Redirect" url="https://es.mydomainNEW.com/{C:1}" redirectType="Temporary" />
                </rule>

Update: If you need to redirect (es/fr/de/hk).mydomainold.com to (es/fr/de/hk).mydomainnew.com. Please try this

      <rule name="rewrite rule" enabled="true" stopProcessing="true">
            <match url="(.*)" />
            <conditions trackAllCaptures="true">
                <add input="{HTTP_HOST}" pattern="([a-zA-Z]{2})\.mydomainOLD.com" />
            </conditions>
            <action type="Redirect" url="https://{C:1}.mydomainNEW.com/{R:0}" redirectType="Temporary" />
        </rule>

Upvotes: 1

iusmar
iusmar

Reputation: 359

For scenario #1:

<rule name="General redirect - all urls - from OLD to NEW" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="^mydomainOLD\.com(.*)$" />
                        <add input="{URL}" pattern="^/en/(.*)" />
                    </conditions>
                    <action type="Redirect" url="http://mydomainNEW.com/{C:1}" appendQueryString="false" redirectType="Permanent" />
                </rule>

For scenario #2: One rule for each country (Fr in this example):

<rule name="France redirect - all urls - from OLD to NEW" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="^fr.mydomainOLD\.com(.*)$" />
                        <add input="{URL}" pattern="^/(.*)" />
                    </conditions>
                    <action type="Redirect" url="http://fr.mydomainOLD.com/{C:1}" appendQueryString="false" redirectType="Permanent" />
                </rule>

Upvotes: 0

Related Questions