Question

I want to create urlrewrite url using appcmd for redirecting all requests from

http://

to

https://

I tried to google but nothing found. Could you provide me some basic example?

Was it helpful?

Solution

Got it working with this:

appcmd.exe set config -section:system.webServer/rewrite/rules /+"[name='http_redirect',enabled='True']" /commit:apphost

appcmd.exe set config -section:system.webServer/rewrite/rules.[name='http_redirect'] /match.url:"(.*)" /match.ignoreCase:true /commit:apphost

appcmd.exe set config -section:system.webServer/rewrite/rules.[name='http_redirect'].conditions/add /+"[input='{HTTPS}',pattern='off']" /commit:apphost

appcmd.exe set config -section:system.webServer/rewrite/rules.[name='http_redirect'].action /+"[type='Redirect',url='https://{HOST_NAME}/{R:1}',redirectType='Found']" /commit:apphost

This link can help on building appcmd scripts.

OTHER TIPS

For the benefit of anyone else looking (as I was) you can do the following to insert your new rule at the start, end or a particular position in the list, as just adding it to the end by default may result in the rule not being hit:

appcmd.exe set config -section:system.webServer/rewrite/rules /+"[@start,name='http_redirect',enabled='True']" /commit:apphost
appcmd.exe set config -section:system.webServer/rewrite/rules /+"[@end,name='http_redirect',enabled='True']" /commit:apphost
appcmd.exe set config -section:system.webServer/rewrite/rules /+"[@2,name='http_redirect',enabled='True']" /commit:apphost

Source:

appcmd.exe set config /?

In my case the syntax is slightly different since I wanted to alter a URL Rewrite rule at the root (server) level.

appcmd set config -section:system.webServer/rewrite/globalRules -[name='myRule'].action.url:http://myRewrite /commit:apphost

It took me a long time to figure out the globalRules instead of rules difference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top