"non-www to www" and "https" rewrite rule in web.config but not localhost ASP.NET MVC -
i have following rewrite rule in web.config
of asp.net mvc 5 project:
<rule name="redirect example.com www.example.com , 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 www , http https (so http://example.com/hey
redirect https://www.example.com/hey
) , works fine. works on localhost
, , can't seem able work around — i've tried negation rules , regular expressions containing |
can't seem able find correct combinations. approaching wrong way?
in conditions
block can use attribute negate="true"
. if attribute set , condition matched, rewrite rule not applied.
the description of negate
attribute iis.net:
a pattern can negated using negate attribute of element. when attribute used, rule action performed if current url not match specified pattern.
since using matchany
, adding additional attribute not match, because @ least 1 of conditions met anyway. recommend using 2 specific rewrite rules logicalgrouping="matchall"
, each 1 responsible single case:
<rule name="enforce https" enabled="true" stopprocessing="true"> <match url="(.*)" /> <conditions logicalgrouping="matchall"> <add input="{https}" pattern="off" /> <add input="{http_host}" matchtype="pattern" pattern="^localhost(:\d+)?$" negate="true" /> </conditions> <action type="redirect" url="https://{http_host}/{r:1}" appendquerystring="true" redirecttype="permanent" /> </rule> <rule name="redirect example.com www.example.com" enabled="true" stopprocessing="true"> <match url="(.*)" /> <conditions logicalgrouping="matchall"> <add input="{http_host}" pattern="^[^www]" /> <add input="{http_host}" matchtype="pattern" pattern="^localhost(:\d+)?$" negate="true" /> </conditions> <action type="redirect" url="https://www.example.com/{r:1}" appendquerystring="true" redirecttype="permanent" /> </rule>
Comments
Post a Comment