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 enters http://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.
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):
Now all HTTP request will redirect to your HTTPS site and will preserve the rest of the URL.
I had the same problem where the
R:1
was dropping my folders. I fixed it like this.I believe AndyH's answer to be the easiest and best way. I have found using the URL rewrite can also conflict with code that may redirect the user to another page. IT commonly broke in our environment. But Andy's solution worked flawlessly. I also think Andy's solution will put less overhead on the server as it doesn't need to examine every url hitting it for possible re-write conditions.
I found a workaround:
Consider what in IIS is consired a website: simply a set of rules, the path in which get files and its bindings.
Furthermore, there's available a function called "HTTP Redirect" (included standardly in IIS), that redirect an host to another, keeping all subdirectory (it makes a relative path). The workaround is to leave just the binding for HTTPS (port 443) in your website, and create another with the binding on HTTP (port 80) and set for this an HTTP redirect to your URL with
https://
.For example, consider a website called
mytest
and its urlshttp://www.mytest.com/
andhttps://www.mytest.com/
. Set for it instead only binding onhttps://www.mytest.com/
, and delete thehttp
binding. Then create a new website with the same local path, calledmytest http
with just a binding over port 80 (http://www.mytest.com/
) and set for this one anHTTP Redirect
tohttps://www.mytest.com/
.Simple and clean, and that should be as fast as directly the https url for the user, because it's just an internal redirect. I hope that can work for you!
I have found that the
syntax will only work for the website's ROOT web.config file.
If the rewrite rule is applied to a virtual web.config file, then use..
The {URL} syntax will include the initial forward slash, the virtual path, and any URL parameters.
Change it to: