locked
IIS URL Rewrite - with multiple domains RRS feed

  • Question

  • User457981472 posted

    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.

    I've also tried an exact match of domain.co.uk to redirect to www.domain.co.uk but this also doesn't redirect.

    Does anyone know where i'm going wrong or if this is even possible or if there is a better way of achieving what I need to do?

    Wednesday, January 20, 2016 10:43 AM

Answers

  • User1278090636 posted

    Hi,

    Since you used <match url=”(.*)”>, it will match all request. You need to add some input rule in <condition> section to filter requests that don’t need redirect.

    You have already added input rule for domain1, so the redirect rule should work fine with domain1.

    I think you can add input rules for other domains or just add a input rule to filter www request using code like below.

    <add input="{HTTP_HOST}" pattern="^www" negate="true" />

     

    Best Regards,

    Jean

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Wednesday, January 27, 2016 4:29 AM

All replies

  • User1278090636 posted

    Hi jamm,

    You can use the redirect rule below to redirect non-www to www.

    <rule name="Redirect to www" stopProcessing="true">

      <match url="(.*)" />

      <conditions trackAllCaptures="false">

        <add input="{HTTP_HOST}" pattern="^domain" />

      </conditions>

      <action type="Redirect" url="http://www.domain.com/{R:1}" />

    </rule>

    Best Regards,

    Jean

    Thursday, January 21, 2016 4:52 AM
  • User457981472 posted

    Hi Jean

    Thanks for the reply, but wouldn't that force everything to the www. of one specified domain?

    As I said in my question I have many domains/websites running off the same pages, so 30 domains point to this website but each displays different content, styles etc dependent on the domain.

    So I want to force each individual domain to the www. version of that domain, not force everything to one particular domain.

    Hope that makes sense?

    Thursday, January 21, 2016 5:38 AM
  • User-2064283741 posted
    Still not sure what you want.

    Can you post urls of what is entered and what you want.
    Post your current url rewrite rule.
    Thursday, January 21, 2016 9:21 AM
  • User457981472 posted

    Hi Rovastar

    Thanks for your reply, I'll try and explain further.

    So 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

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

    Friday, January 22, 2016 7:42 AM
  • User1278090636 posted

    Hi jamm,

    Sorry about my mistake, I did some change to your code. You can take it as reference.

    <rewrite>
        <rules>
            <rule name="CanonicalHostNameRule1">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www\.domain1\.com$" negate="true" />
                    <!-- rules for other domains(domian2,domain3..)should be added here -->
                </conditions>
                <action type="Redirect" url="http://www.{HTTP_HOST}/{R:1}" />
            </rule>
          </rules>
    </rewrite>

    Best Regards,

    Jean

    Monday, January 25, 2016 4:39 AM
  • User457981472 posted

    Hi Jean

    Sorry one more question about it. While we are testing/building each of the websites we put them on a test domain, which is a sub domain e.g. sub.domain.com, I guess this would break the sub domains?

    I've just tried what you suggested but it caused the site not to work.

    In Chrome it redirected to www.www.domain.com, in IE it went to the correct URL but I got an error saying 'This page can’t be displayed'. Any ideas?

    Monday, January 25, 2016 5:16 AM
  • User1278090636 posted

    Hi,

    Since you used <match url=”(.*)”>, it will match all request. You need to add some input rule in <condition> section to filter requests that don’t need redirect.

    You have already added input rule for domain1, so the redirect rule should work fine with domain1.

    I think you can add input rules for other domains or just add a input rule to filter www request using code like below.

    <add input="{HTTP_HOST}" pattern="^www" negate="true" />

     

    Best Regards,

    Jean

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Wednesday, January 27, 2016 4:29 AM