HTTP to HTTPS redirect on IIS for multiple websites
By Mirek on (tags: 500.52, HTTPS, iis, Rewrite, categories: infrastructure, web)Today a quick hint and also a reminder for myself on how to handle HTTP to HTTPS redirects on IIS for more than one hosted website and fix 500.52 - URL Rewrite Module Error.
Say you have two websites running on IIS, let it be named website.main and website.blog. Note that these names are the website names on IIS tree, not the actual websites urls.
Now let’s say you already have https bindings configured for both websites. Next you want to configure http to https redirection so whenever someone request them by http it will redirect him automatically to https version of the website.
All you need to do is to modify a applicationHost.config file which resides in
%windir%\System32\inetsrv\config
and for each of websites you need to add following section
<location overridemode="Allow" path="website_name">
<system.webserver>
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopprocessing="true" enabled="true">
<match url="(.*)">
<conditions>
<add pattern="^OFF$" input="{HTTPS}">
</add>
<action url="https://{HTTP_HOST}/{R:1}" redirecttype="Permanent" type="Redirect">
</action>
</conditions>
</match>
</rule>
</rules>
</rewrite>
</system.webserver>
</location>
replacing website_name path with your website name. So we have to put one with name website.main and one with website.blog.
Now, it all would be fine except when you try to reach one of the websites you get IIS error
500.52 - URL Rewrite Module Error.
This is caused by the fact that IIS or rather Rewrite Module I guess, sees two rewrite rules with the same name
< rule name="Redirect HTTP to HTTPS"
and apparently rule names must be globally unique even if they sit in separate location sections. To fix it we need to rename these rules
<rule name="Redirect HTTP to HTTPS for website.main"
<rule name="Redirect HTTP to HTTPS for website.blog"
And that’s it.
Cheers