Reputation: 21
I am using a web.config to redirect all non www to www which is working fine. How do I add a rule to ignore all subdomains so http://example.com redirects to http://www.example.com but any subdomain http://*.example.com or http://one.example.com do not redirect to www. (I cannot add the list of subdomains).
<rule name="Redirect to www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}" redirectType="Permanent"/>
</rule>
Upvotes: 0
Views: 159
Reputation: 30
I think Joey's answer only covers the cases where domains are ending in .com and can't handle cases like .co.uk .co.za .com.au etc. You can extend the regular expression a little more but allowing for different types of domains using [a-z] will end up redirecting the subdomains as well, which is what you don't want according to the question.
So in order to achieve this, you'll have to handle the specific cases that you think can arise in your situation.
<rule name="Redirect to www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^([a-z-]+[.](com|co|ca|net)([.](uk|sg|au)){0,1})$" negate="false" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}" redirectType="Permanent"/>
</rule>
You can modify the regular expression to your liking.
Upvotes: 1
Reputation: 20067
You could try to with the following code:
<rules>
<rule name="Redirect to www">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
<add input="{HTTP_HOST}" pattern="^one\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}"/>
</rule>
</rules>
Upvotes: 0