I have the following rewrite rule in the web.config
of my ASP.NET MVC 5 project:
<rule name="Redirect example.com to www.example.com and enforce https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
The rule redirects non-www to www and http to https (so something like http://example.com/hey
will redirect to https://www.example.com/hey
) and works fine. It however also works on localhost
, and I can't seem to be able to work around it — I've tried negation rules and regular expressions containing |
but can't seem to be able to find the correct combinations. Am I approaching this the wrong way?