Search code examples
urlhttp-redirectiisurl-rewritinghost

How to dynamically rewrite/redirect url


I got a little bit of a complicated problem here. I searched other topics but did not come across a solution. I have an website-url, say foo.com, and an url called bar.com. They share the same code. Now everything works great, except for the redirecting the URL part. I also do want to let you guys know I've had no experience with URL Rewriting, so keep it simple please.

Foo.com existed first, and got redirected from foo.com to www.foo.com via a CanonicalHostNameRule, redirecting the pattern (.*)to http://www.foo.com/{R1}, which works great for that domain, but not for the bar.com domain.

Below are my full rewrite rules for the website: enter image description here

And these are my web.config rewrite rules:

<rules>
                <clear />
                <rule name="LetsEncrypt Rule" stopProcessing="true">
                    <match url="^\.well-known.*$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="None" />
                </rule>
                <rule name="CanonicalHostNameRule1" enabled="false" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="^www.\foo.be$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.{R:2}/{R:1}" />
                </rule>
                <rule name="Rewrite legacy url's" patternSyntax="ECMAScript">
                    <match url="^.((?!nl-BE).)+$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
                    </conditions>
                    <action type="Redirect" url="{C:1}" redirectType="Permanent" />
                </rule>
                <rule name="One homepage - redirect /home/">
                    <match url="^([a-z][a-z]-[A-Z][A-Z])/home/?" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="{R:1}/" redirectType="Permanent" />
                </rule>
                <rule name="One homepage - redirect root to language">
                    <match url="^\d*$" negate="false" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="/nl-BE/" redirectType="Permanent" />
                </rule>
                <rule name="Add trailing slash">
                    <match url="^([^.]*[^/])$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="{R:1}/" redirectType="Permanent" />
                </rule>
                <rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
                </rule>
            </rules>

Can you guys please get me on the right track on how to fix this? This is all to complicated for my small brain. Thanks in advance guys!


Solution

  • you could try below url rewrite rule:

     <rule name="Canonical3" enabled="true" stopProcessing="true">
             <match url=".*" />
             <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{HTTP_HOST}" pattern="^([a-z0-9]+[.]com)$" />
             </conditions>
             <action type="Redirect" url="http://www.{C:1}/{R:0}" redirectType="Permanent" />
           </rule>
    

    this will work with both the domain name.