IIS Rewrite rules for languages and new domains

霸气de小男生 提交于 2020-06-29 03:37:08

问题


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.


回答1:


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 senario #2,

   <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>


来源:https://stackoverflow.com/questions/62579295/iis-rewrite-rules-for-languages-and-new-domains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!