IIS HTTP to HTTPS relative redirect
Solution 1
I found a way to do this, and you don't need the Rewrite module for it.
The following worked for me on Windows 8 (IIS 8.5):
- Remove the HTTP binding from your site (leave HTTPS in place)
- Add another site
- Make sure that the new site has HTTP binding
- Configure HTTP Redirect as shown:
Now all HTTP request will redirect to your HTTPS site and will preserve the rest of the URL.
Solution 2
Change it to:
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
Solution 3
I had the same problem where the R:1
was dropping my folders.
I fixed it like this.
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
appendQueryString="false" redirectType="SeeOther" />
</rule>
Solution 4
I can't comment yet or I'd leave this as a comment under AndyH's answer. The solution was correct, though I hit a single further snag (likely tied to the use of Adobe's Coldfusion server). I wanted to share some further research I had to do for any other unfortunate soul who may run into it.
Once set up, the redirect would always end at this url:
https://xxx.xxx.com/jakarta/isapi_redirect.dll
The fix for this was found in an Adobe thread (https://forums.adobe.com/thread/1034854): I had to change an application pool's settings as follows:
Real site (HTTPS binding only, actually contains code and virtual directories)
Application pool's Advanced Settings: Enable 32-Bit Applications : False
Http_Redirect site (HTTP binding only, is a blank shell of a folder with no directories)
Application pool's Advanced Settings: Enable 32-Bit Applications : True
EDIT: Another detail, tied to query string preservation:
Per suggestion in this post (http://www.developerfusion.com/code/4678/permanent-301-redirect-with-querystring-in-iis/)
Add $S$Q
at the end of the domain and make sure the box for Redirect all requests to exact destination
is checked. Then it will save the query string as well.
Related videos on Youtube
user2234612
Updated on September 18, 2022Comments
-
user2234612 almost 2 years
I recently got a SSL certificate for my website and want to redirect all traffic to HTTPS. I got everything to go to
https://mydomain.com
but if someone entershttp://mydomain.com/anotherpage
it drops the other page and just takes the user to the home page.My rule in my
web.config
file looks like this:<rule name="HTTP to HTTPS redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions> <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" /> </rule>
I also tried
https://{HTTP_HOST}{REQUEST_URI}
without any success. Can anyone tell me what I need to do to make the website redirect to the proper HTTPS version of the page? I have a feeling it has something to do with the pattern, but I can't seem to figure out the syntax. -
Riz almost 8 yearsawesome it worked! was also able to bind an ip address to redirect to https! Thanks!
-
Zapnologica almost 8 yearsWhat is the key change here?
pattern="^OFF$"
?