Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL Rewrite - with multiple domains

I have a website/application where the same pages run many websites (depending on the URL the pages display different logo/css/content etc). I am trying to force each website domain to redirect to the www. verison of the domain.

I have used the default Canonical Rule to start with in URL Rewrite then just changed the Match URL Pattern from:

(.*)

To:

(^domain*)

So that it will only do this for the non-www. version of the current domain and not match for all domains.

My plan is to create one of these for each domain that uses the system to redirect each one to the www. version.

When I test the pattern in IIS the rule matches as (I think) it should do. But when I go to the website it doesn't redirect, it just stays on the non www. version.

Does anyone know where i'm going wrong or if this is even possible?

Thanks J.

UPDATE Thought i'd try and give a better explanation:

I have one website setup in IIS. The pages of this website run many websites of the same type, the style, logo and products on each website change depending on the URL.

So say I have the following bindings setup on the website:

www.domain1.com, domain1.com, www.domain2.com, domain2.com, www.domain3.com, domain3.com

The website will serve up a different looking website, with different content, styles etc depending on the URL.

If I then use the default 'Canonical domain name' rule in URL Rewrite you have to pick one domain to forward to, so if i chose www.domain1.com it adds rule:

<rewrite>
    <rules>
        <rule name="CanonicalHostNameRule1">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www\.domain1\.com$" negate="true" />
            </conditions>
            <action type="Redirect" url="http://www.domain1.com/{R:1}" />
        </rule>
      </rules>
</rewrite>

This then forwards everything to www.domain1.com. So if a user visits domain2.com or www.domain2.com they will get redirected to www.domain1.com.

I need it so that people get redirected as follows:

domain1.com > www.domain1.com

domain2.com > www.domain2.com

domain3.com > www.domain3.com

When testing new websites to add to the existing ones, we set them up on a sub domain, so I also require sub domains to continue to not be re-directed.

But the issue is with doing this within the same website/web.config file.

like image 519
Jammer Avatar asked Jan 19 '16 16:01

Jammer


People also ask

Can I connect 2 domains for one website?

With most registrars, it's easy to forward multiple domains to your website so you can simply create one site and then redirect visitors who type one of your other domain names to that one website.

How does IIS URL Rewrite work?

The concept of URL rewriting is simple. When a client sends a request to the Web server for a particular URL, the URL rewriting module analyzes the requested URL and changes it to a different URL on the same server.


Video Answer


1 Answers

Try the following (credit to @Atashbahar at https://stackoverflow.com/a/31955179/2664650):

<rewrite>
    <rules>
          <rule name="Add WWW" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions>
             <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
          </conditions>
          <action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
       </rule>
    </rules>
</rewrite>
like image 97
cl0rkster Avatar answered Oct 05 '22 18:10

cl0rkster