web.config force redirect of www to non-www with https - redirect

So currently my main domain works fine, if I go to www.domain.com it redirects to https://domain.com
my sub-domains are the issue. I have a wildcard SSL as well for *.domain.com
if I go to www.sub.domain.com, it redirects to https://www.sub.domain.com which has an invalid SSL cert and I am trying to get it to load FROM: www.sub.domain.com to https://sub.domain.com but am having some issues. Godaddy was no help as it seems most of them are "New". Hosting with Plesk unfortunately. Currently what I have for my web.config is:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Check for domains hosting settings like "preferred domain" and 301 redirect to HTTPS:
If you have no 301 redirect to HTTPS you can just delete this web.config.

Wildcard SSL Certificate cannot work on second level sub-domain when you have installed certificate for first level (for example: level3.level2.level1.domain.com).
You cannot use WWW before your sub-domain. I suggest you to refer my previous answer on the same issue.
https://stackoverflow.com/a/37959152/4649681
Hope this will help.

<rewrite>
<rules>
<clear />
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^yourwebsite\.com$" negate="true"></add>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://yourwebsite.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

Related

"This website is temporarily unavailable" after trying to redirect from non-www to www (godaddy)

I tried to redirect all my traffic from non-www to www, but it resulted in my non-www page being redirected to a seeminly random IP saying just "This website is temporarily unavailable, please try again later."
It has been about 12 hours where my site has been down. (I expected it working as before until the DNS records were updated) So I am getting a bit worried.
Here is how my redirect settings look like:
DNS settings look like this:
A # Forwarded
A # Forwarded
CNAME www badgag.trafficmanager.net
I also added this in web.config if that is relevant:
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false"/>
</rule>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^badgag.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.badgag.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

How to redirect all traffic to https & non-www in web.config?

I'm using an SSL certificate and IIS. In my web.config I want to redirect all website traffic in the following order:
all http --> https
all www --> non-www
You want to use the redirect module in order to do this. This intercepts the request as it's coming in to the server and changes it according to your instructions.
Here is an action to redirect all to https:
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
and here is one to redirect www to non-www:
<conditions> <add input=”{HTTP_HOST}” pattern=”^example\.com$” negate=”true” /> </conditions> <action type=”Redirect” url=”http://example.com/{R:1}” />
There are plenty of articles around the web and StackOverflow when you include both "web.config" and "redirect" in your search.
Make sure that when you set up the rules you only use stopProcessing="true" on the final rule. If that appears on the first rule then the second rule will never execute.
i find the answer
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="NonWwwRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.yoursit\.com$" />
</conditions>
<action type="Redirect" url="http://yoursite.com/{R:1}" />
</rule>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<system.webServer />

Need to redirect non-www and/or http to www AND https

Plain asp website. Need to redirect non-www to www AND http to https.
Found another post that was close but doesn't completely work. It works for root entries only but if there is anything beyond the root site name then the redirect does not work at all.
For example --
non-www will redirect properly to https and www
non-http will also redirect properly to https and www
But domain.com/categories.asp?cat=130 will not redirect and will simply stay on the non-https, non-www page.
Any fix for this redirect?
Here is the code that I used:
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
OK -- I added this after my http to https and this works:
--
<rule name="Canonical name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
</conditions>
<action type="Redirect" url="https://www.domain.com/{R:1}" redirectType="Permanent" />
</rule>

HTTP to HTTPS Redirect - IIS 8.5 not working properly

I've read a number of posts here on SO as well as on the 'net (IIS blogs, etc.). I'm trying to force all connections going from domain.com to www.domain.com and at the same time forcing the request from HTTP to HTTPS.
I'm using this set of rules and rewrites but the only thing happening is that it's redirecting fine but not redirecting to SSL.
<!-- Redirect to HTTPS -->
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^mydomain.com$" ignoreCase="true" negate="false" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.mydomain.com/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol" defaultValue="http">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
What am I doing wrong?
Main blog reference: http://weblogs.asp.net/owscott/url-rewrite-protocol-http-https-in-the-action and this SO post - web.config redirect non-www to www
We faced same issue while redirecting from http to https using URL Rewrite module, But after switching off the require ssl module within IIS did the trick.
Go to SSL Settings of website
Uncheck require SSL checkbox.
Apply settings and Restart iis
Edit: So I found this blog post: http://www.meltedbutter.net/wp/?p=231 and gave it a try and voila! Worked like a charm. Not sure why this worked over the rules posted above but in my case the below is working and successfully taking all non-www traffic and redirecting it to both www and https.
<!-- Redirect to HTTPS -->
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://www.domain.com/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
This is a long shot for some cases but I had deleted my port 80 bindings for the website as I only wanted SSL / port 443. So from what I can tell is Port 80 binding is also needed for the the rewrite to work correctly.
In my case, I did everything #Valien said and lots of other tries but it did not work. finally I found the problem. It was because I was using 2 rules. First rule was rewriting to NodeJs server and the second to redirect http to https. I changed the order and now it is working correctly.
The config with the correct order in case more than 1 rule is applied:
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://mywebsite.com/{R:1}" redirectType="SeeOther" />
</rule>
<rule name="ReverseProxyToLocalhost:3000" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:3000/{R:1}" />
</rule>
</rules>
</rewrite>

iis8 rewrite web config for https multiple applications

Been struggling with this and can't get it to work. Can't find any answers online.
I have tried the web.config in the root (www.example.com) and in the application (www.example.com/APP_SSL)
Basically, I have an iis 8 server with multiple websites.
For one particular website. (say example.com) I want suborders to always redirect to https. Specifically these sub folders are Applications.
I am also trying to redirect example.com -> www.example.com at the same time.
Here is what I want to accomplish: (sorry if I am too verbose)
example.com/APP_SSL/... -> httpS://www.example.com/APP_SSL/...
www.example.com/APP_SSL/... -> httpS://www.example.com/APP_SSL/...
BUT for other files and applications just be sure we have the www part.
I can live without this stuff as long as the above works.
example.com -> http://www.example.com
www.example.com -> http://www.example.com (yes I know this is always true)
example.com/file.htm -> http:/www.example.com/file.htm
www.example.com/file.htm -> http:/www.example.com/file.htm
example.com/APP/ -> http://www.example.com/APP/...
www.example.com/APP/ -> http://www.example.com/APP/...
I have tried this, but it really does not do anything that I want.
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com/APP_SSL$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.example.com/APP_SSL/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
Putting this in my /APP_SSL/Web.Config gets me most of what I want with the exception of the example.com -> www.example.com
<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}/APP_SSL/{R:0}" redirectType="SeeOther" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>