Remy
Remy

Reputation: 12703

IIS CanonicalHostNameRule for different wrong subdomains

I currently have this CanonicalHostNameRule.
We have multiple top-level domains (.ch and .de), that is why we don't use the default CanonicalHostNameRule from IIS.

<rule name="RedirectToWWW" enabled="true" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^(?!www)(\S+)\.(de?|ch)$" />
  </conditions>
  <action type="Redirect" url="https://www.{C:0}/{R:0}" />
</rule>

This works for

http://supertext.ch

But not for

http://holdrio.supertext.ch
http://ww.w.supertext.ch
http://www.mail.supertext.ch

and I really would like something that redirects any subdomain to www.supertext.ch or .de respectively. Any ideas?

Upvotes: 0

Views: 155

Answers (1)

Heini
Heini

Reputation: 161

You could use following pattern: ^(?!www\.supertext\.[de|ch])(\S+)\.(de|ch)$

The results would be for example:

holdrio.supertext.ch ->

  • {C:0}: holdrio.supertext.ch
  • {C:1}: holdrio.supertext
  • {C:2}: ch

holdrio.supertext.de->

  • {C:0} holdrio.supertext.de
  • {C:1} holdrio.supertext
  • {C:2} de

supertext.ch->

  • {C:0} supertext.ch
  • {C:1} supertext
  • {C:2} ch
<rule name="RedirectToWWW" enabled="true" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(?!www\.supertext\.[de|ch])(\S+)\.(de|ch)$" />
    </conditions>
    <action type="Redirect" url="https://www.supertext.{C:2}/{R:0}" appendQueryString="true" redirectType="Permanent"/>
</rule>

Note: www.supertext.ch.supertext.ch is not covered

I hope this works for you.

Upvotes: 1

Related Questions