iis8 rewrite web config for https multiple applications - web-config

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>

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>

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

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>

IIS 8.5 URL Rewrite force redirect http --> https for non www

I am starting with the rewrite module have a problem with a "simple" IIS 8.5 URL Rewrite redirect http --> https for a non www site.
Problem: If the domain matches the action url parameter i always get "http://" and not "https://".
This is my rule:
<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}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I can't post several links so "domain" = crm.test.com. The site with the rewrite rule is bound to "domain"=crm.test.com on port 2080.
I want to redirect "http://" to "https://", but i always get "http://" as location in response:
HTTP/1.1·302·Redirect
Connection:·close
Content-Length:·176
Date:·Thu,·15·Jan·2015·08:21:21·GMT
Location:·http://domain/ <--
Content-Type:·text/html;·charset=UTF-8
Server:·Microsoft-IIS/8.5
X-Powered-By:·ASP.NET
I tried the following action url parameter:
Not working:
"https://{HTTP_HOST}/{R:1}" -> http://domain/
"https://domain/{R:1}" -> http://domain/
"https://{HTTP_HOST}:443/{R:1}" -> http://domain/
"https://{HTTP_HOST}/1" -> http://domain/1/
Working:
"https1://{HTTP_HOST}/{R:1}" -> https1://domain/
"https://{HTTP_HOST}:444/{R:1}" -> https://domain:444/
"https://test.domain.com/{R:1}" -> https://test.domain.com/
"https://www.google.com/{R:1}" -> https://www.google.com/
I found "URL Rewrite on IIS from http to https is not working,", but this doesn't solve my problem.
Did i missed something?
The following works for us:
<rule name="HTTP Redirect to HTTPS" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
O. k., i found the solution - the link translation is caused by a tmg 2010 in front of the iis. The tmg has a bug where the link translation resets the https links to http in case of a 301. Problem an solution are described here:
http://blog.sanibellogic.com/2008/09/default
http://support.microsoft.com/kb/924373
Thanks everybody.
You can also config the redirect domain with or without www using URL rewrite. The SSL cert only include: www FQDN.
web.config example (domain example is: sysadmit.com):
<rewrite>
<rules>
<clear />
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.sysadmit.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Extracted from: http://www.sysadmit.com/2017/05/windows-iis-redirigir-http-https.html

How to add RedirectType to External config file in asp.net

I have a separate .config file in the application root directory which contains Mapped URLS for redirect and referenced this .config file in web.config for 301 Permanent Redirect! This works fine.
See Reference Link
Now, i also want to add some links which will redirected as 302 status code. How to add 302 redirect in external .config file and redirect accordingly.
rewritemaps.config
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/oldcellphone" value="/newcellphones.aspx" />
</rewriteMap>
</rewriteMaps>
Can we specify the Redirect Type i.e 301/302 in this file?
web.config
<system.webServer>
<rewrite>
<rewriteMaps configSource="rewritemaps.config">
</rewriteMaps>
<rules>
<rule name="Redirect rule1 for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
NOTE:Currently all links from file 'rewritemaps.config' are set to 301 Status in web.config.
Can we add as following in rewritemaps.config and redirect accordingly:
<add key="/oldcellphone" value="/newcellphones.aspx" [RedirectType=301] />
<add key="/oldphone" value="/newphones.aspx" [RedirectType=302] />
There are about 1000 links of 301 Status and about 400 links for 302 Status. If its not possible in external file(rewritemaps.config) then please suggest preferred way to do?
Update:
Can you help me to redirect to another site(different domain) if specific string match in requested URL .
Eg: if the requested URL contains "/hm1" then redirect to different site. i.e http://www.google.com
Web.config
<rule name="othersite" stopProcessing="true">
<match url="^/hm1$" />
<action type="Redirect" url="http://www.google.com" redirectType="Found"/>
</rule>
.aspx
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="/hm1">other site (http://www.google.com)</asp:HyperLink>
Can you add RedirectType into a Rewrite Map? No, unfortunately not.
To achieve what you're trying to do you're going to need to create two Rewrite Maps and two Rewrite Rules - one for 301 redirects and one for 302 redirects.
Here's an example of how that might look:
<rewrite>
<rules>
<rule name="301Redirects" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
<conditions>
<add input="{301Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
</rule>
<rule name="302Redirects" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Found" />
<conditions>
<add input="{302Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="301Redirects">
<add key="/oldurl" value="/newurl" />
</rewriteMap>
<rewriteMap name="302Redirects">
<add key="/oldcellphone" value="/newcellphones.aspx" />
</rewriteMap>
</rewriteMaps>
</rewrite>

iis url redirect http to non www https

i need to redirect from
www.domain.de to https://domain.de
-works
http://www.domain.de to https://domain.de
-works
http://domain.de to https://domain.de
-does not work
rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I think this will work for you, the search pattern has the optional www and redirects using the back reference C:2, the rule has a condition to only run against non https.
This is the pattern:
"^(www\.)?(.*)$"
where:
{C:0} - www.domain.de
{C:1} - www.
{C:2} - domain.de
Here's the rule in full:
<rewrite>
<rules>
<rule name="SecureRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The accepted answer doesn't handle the special case https://www.domain.de.
These rules do the complete job:
<rewrite>
<rules>
<rule name="Redirect to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Special case for HTTPS with www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
If you want to redirect www to non www:
Add DNS entry for www.yourdomain.com to refer to your server's public IP
Then you need to Edit Bindings of your website and "Add Binding" www.yourdomain.com
Add a rewrite rule to your website using iis:
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
Reference: http://madskristensen.net/post/url-rewrite-and-the-www-subdomain
If you want something more flexible than for your three examples, change your HTTP_HOST pattern to : \w+\.\w+$. That would work for all three examples plus anything else, like subdomain.abcdef.domain.de.
If you use this regex either encase it in parenthesis or change C:1 to C:0 in your action.
This is what worked for me:
<rule name="NameRule1" stopProcessing="true" enabled="true" >
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.com/{R:1}" />
</rule>
I got it from: https://medium.com/iis-and-windows-server/redirect-url-from-www-to-non-www-in-iis7-5-4d2909b9704
Redirecting https://www.example.com to https://example.com I had to do 2 steps
Select the website, click "URL Rewrite" -> Click "Add Rule" -> Choose "Cononcial domain name" -> select your domain (without the www) -> make sure rule is at the top -> highlight the rule and click "Edit" -> and update "Redirect URL" at the end of the rule to include the 's' - https://example.com/{R:1}
I had to also create a new website with the domain www.example.com, new app pool and select the SSL cert and point it to a folder containing the following (for some reason StackOverflow wont display the code I pasted below for the rewrite rule but just google IIS rewrite rule)
After doing both steps only then does it work as desired
Tried many of solutions but below works for me:
Right click on Domain and Add a Site bindings, add one more domain with www:
2. Restart services. it should work.
These rewrite rules matches the following URL's:
www.example.com
http://www.example.com
https://www.example.com
They will all redirect to: https://example.com
These rewrite rules may redirect twice because of the separate rules. (I'm a newbie with regex)
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://example.com{PATH_INFO}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>