.Net web.config match base URL

Viewed 575

We have a CMS where we host multiple sites under the various subdomains but with the same main domain:

Example:

sub1.mysite.com

sub2.mysite.com

This being the same application they share the same web.config. So when I try to write a rewrite rule and a similar match exists in another subdomain site, the rewrites take affect for all sites.

What I want is to match on the base level domain (mysite.com or sub.mysite.com) and make my rewrite rules?

All the answers I find say to use something like

 <match url="^(.*)$" ignoreCase="true" />
 or <match url="^mypage/?(.*)$" />
 or <match url="^mypage/mysubpage//?(.*)$" />

But the problem with looking at any of these is that the carat '^' is kind of a wild card and not domain/subdomain specific.

Lets say I want to have a rewrite rule for

mysite.com/about-us

and a sub domain site has a page named the same

sub1.mysite.com/about-us

BUT I ONLY want to a rewrite for the main mysite.com/about-us and NOT have the sub1.mysite.com/about-us site have the same rewrite rule.

How would I tell it that I only want mysite.com/about-us to rewrite to mysite.com/about/about-us?

1 Answers

If you include an "HTTP_HOST" on your rule, you can restrict it to your main URL.

eg. in the below, I have a main domain setup as replicant.test and the redirect will NOT work on a sub-domain like "site1.replicant.test.

        <rule name="Main site redirect" stopProcessing="true">
            <match url="^article/([0-9]+)/([_0-9a-z-]+)" />
            <action type="Rewrite" url="article.aspx?id={R:1}&amp;title={R:2}" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^replicant\.test$" />
            </conditions>
        </rule>

The main redirect rule was taken from https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module.

Related