Reputation: 133
I am redirecting all my websites non-www versions of domains to www version.
However, the side-effect is that it is also redirecting sub-domains with www
e.g. api.example.com to www.api.example.com
Which is not what I wanted. I need all the sites with sub-domains to NOT prepend the www.
Here are my current rules:
<system.webServer>
<rewrite>
<globalRules>
<clear />
<rule name="non-www to www" enabled="true" stopProcessing="false">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" />
</rule>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" /> <!-- Require SSL must be OFF in the site settings -->
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</globalRules>
</rewrite>
</system.webServer>
Is it possible to make a rule that only redirects to www.example.com if the input doesn't start with 'www' AND it's NOT a sub-domain?
Anything with a sub-domains must not prepend the www
Please help as I'm already trying last two days without success.
Thank you in advance!!
Upvotes: 1
Views: 213
Reputation: 8736
You need to change your regexp to:^[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})?$
and this regexp will catch only second-level domains.
I took it from this answer: https://stackoverflow.com/a/21174423/1954204
At the end your rule will be like that:
<rule name="non-www to www and https" enabled="true" stopProcessing="false">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})?$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}" />
</rule>
Upvotes: 2