IIS default website conditional redirect upon domain name - redirect

I have many websites hosted on the same IIS. When I stop one of them, and trying accessing it, it renders the default website of IIS. i.e iisstart.htm.
What I need to have is, for example, when example1.com has been stopped and trying to access its home page or better any URL on it i.e example1.com/some/path it redirects to an other defined page on the root of the default website, such as, example1.html. Also, I need the solution to be applied as many as websites that I have, i.e example2.com, example3.com and so on.
I have tried the following web.config settings, but it does not work:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="conditional products redirect" enabled="true">
<match url="^example1(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="/example1.html" />
</conditions>
<action type="Redirect" url="/example1.html" appendQueryString="false" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

http with different app ports to https with same port

since hours I try to get an IIS problem solved. I have different application webservers, which will be reached by different ports. subdomain.domain:11701 looks for the webpage created by one app webserver subdomain.domain:11702 looks for another webpage by another app webserver
certs are created ssl is working
http://subdomain.domain.com works
https://subdomain.domain.com works
http://subdomain.domain.com:11701(or 11702, 11703, etc) works
https://subdomain.domain.com:11701 doesn't work (page not found).
IIS pattern test for
<add input="{HTTP_HOST}" pattern="^.+$" />
gives me the right output {C:0} as subdomain.domain.1170n
as I understand should
`<action type="Redirect" url="https://{C:0}" appendQueryString="false" />`
with {C:0} as the full URL including the port doing the rewriting to https, or do I have here some misconception
Full web.config
`<?xml version="1.0" encoding="UTF-8"?>`
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to https" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^.+$" />
</conditions>
<action type="Redirect" url="https://{C:0}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
`

Rewrite subfolder only (HTTP to HTTPS) redirects to site root

I'm trying to rewrite HTTP to HTTPS for a specific subfolder on my IIS 8.5 web server but it's not working. I've read countless other solutions and blog postings but nothing I've tried works.
http://domain.example.com/one/two/three/
should redirect to... (same url but using https)
https://domain.example.com/one/two/three/
but instead is redirecting to... (site root using https)
https://domain.example.com
loading... (desired url with https)
https://domain.example.com/one/two/three/
also redirects to... (site root using https)
https://domain.example.com
it's removing the subfolders from the url.
This folder needs to also be protected with Windows Authentication, which I can get to work but the https redirection is failing with or without the authentication enabled so I don't think that's the cause.
Within IIS I selected the desired subfolder (/three/ in the example above) and created the Rewrite rule there.
<rewrite>
<rules>
<clear />
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
This should of course work with any files and folders contained within the desired subfolder. (/three)
I tried this and it redirects to the apparent correct url but gives the "too many redirects" error:
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="SeeOther" />
</rule>
You should avoid doing this:
Within IIS I selected the desired subfolder (/three/ in the example
above) and created the Rewrite rule there.
Instead setup the rewrite rules in Web.config at the application root. You can redirect a specific folder to HTTPS by including it in the match parameter as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect Subfolder" stopProcessing="true">
<match url="^one/two/three/" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Please note that this is a minimal Web.config file that does what you are looking for. If your application already contains a Web.config in the root folder, then you will have to merge the above into your solution.

IIS Redirects to multiple domains and preserves the request file path and names

I'm running IIS 8, Windows Server 2012.
My website has many sub-directories. An ideal url will look like:
oldsite.com/subdir/subdir/pages/default.aspx
I need to redirect (301) visitors to different domains and need to preserve the rest of the original url's file path (everything from the 2nd to the last slash "/").
I have a map of my redirects in an excel sheet, it looks like below:
oldsite.com/subdirA/subdirB/pages/default.aspx -> newsiteA.com/subdirC/subdirD/pages/default.aspx
So you see the whole thing has to change except the last part.
My web.config looks like this:
<rewrite>
<rewriteMaps>
<rewriteMap name="RedirectsCollectionParshareSites">
<add key="/subdirA/subdirB/" value="/subdirC/subdirD/" />
<!--will have many of these add keys-->
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rule1 for RedirectsCollectionParshareSites">
<match url="^(.+)" />
<conditions>
<add input="{RedirectsCollectionParshareSites:{REQUEST_URI}}" pattern="^([^/]+)/([^/]+)(.*)" />
</conditions>
<action type="Redirect" url="http://newsiteA.com/{C:1}{C:3}" appendQueryString="true" />
</rule>
</rules>
My wish is to have many "add keys" - with the mapping from my excel sheet, and per new domain, I will have a new "rewriteMaps" section, with it's own "action".
My question is, is this the proper way of using this module? Any other suggestions?
Remember, I have about a thousand of these redirects to implement.
Any help would be appreciated.
I wasn't able to use the rewritemaps approach, but was able to do it by doing a rule per site:
<rule name="Rule:tes" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^(\/subdirA)(\/subdirB)(.*)" />
</conditions>
<action type="Redirect" url="http://newsite.com/subdirC/subdirD/{C:3}" />
</rule>
It's harder for admins to update, but it works.

301 Redirect one domain to another using web.config

I have multiple domains pointing to one hosting location.
I wish to establish one of the domains as my main domain and therefore I wish to perform a 301 redirect to this main domain whenever a user accesses my site from a secondary domain.
For example:
www.example.com
This is my main domain. I want all other domains associated with my site to redirect to here.
If a user comes in on:
www.test.com or
www.test.com/anypage
etc.
Then I want the user to be redirected to the example version of that page.
How do I do this using the web.Config file of my application? The reason I ask is that usually my web hosting provider has a tool in their back office that allows me to setup this redirect however, our client has opted for a different hosting provider that do not provide such a tool.
I have attempted to do this redirect using the following code but it doesn't seem to work:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^test\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}}" redirectType="Permanent" />
</rule>
My application is an Umbraco powered site and so has several system.webServer entries in the web.config file. It may just be the case that I have entered this code in the wrong place but any help here would be greatly appreciated as I am only used to doing 301 redirects in .htaccess files.
This is not really that umbraco related but I think what you want to do is this:
<rewrite>
<rules>
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Match all urls unless the host name part is exactly www.example.com - and redirect those to www.example.com/whatever.

IIS 7.5 HTTP to HTTPS redirect

I am trying to have all HTTP requests redirect to HTTPS.
Here is my web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read, Execute, Script" />
</system.webServer>
<system.webServer>
<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" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I found this sample in another post on Stackoverflow:
How to force HTTPS using a web.config file
When I save that file and try to access my site using http it does not redirect to https.
I thought that maybe the file was being ignored so I typed some incorrect syntax into the web.config then I got a 500 error- meaning that it is indeed looking at the web.config file.
Am I misunderstanding what the above configuration is supposed to do? I want to redirect all HTTP request to HTTPS.
The site that is being redirected to is a virtual directory for Tomcat if it makes a difference.
Assuming you have URL Rewrite Installed. Click here for info/installation.
Make sure you have the following configured within the URL Redirect in the IIS Manager.
Match URL Section
Requested URL: Matches the Pattern
Using: Regular Expressions
Pattern: (.*)
Make sure Ignore Case is checked
Conditions Section
Logical Grouping: Match All
Input: {HTTPS}
Type: Matches the Pattern
Pattern: ^OFF$
Action Section
Action type: Redirect
Action Properties
Redirect URL: https://{HTTP_HOST}/{R:1}
Make Sure 'Append Query String' is checked
Redirect Type: See other (303)
Just Try this:
<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="SeeOther" />
</rule>