How can I redirect with Url rewrite e.g. from https://www.contoso.com to another server? - redirect

How can I redirect with url rewrite e.g. from https://www.contoso.com to another server e.g. to https://www.testpage.com

You can use iis url rewrite for redirection, and use this rule:
<rule name="test">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.contoso.com$" />
</conditions>
<action type="Redirect" url="https://www.testpage.com/{R:1}" />
</rule>

Related

conditional redirect in web.config

I have a web.config redirect section that is intended to redirect HTTP requests to HTTPS. Here is the redirect section in my web.config file:
<rewrite>
<rules>
<rule name="Redirect HTTP 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>
<outboundRules>
<rule name="Add the STS header in HTTPS responses">
<match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
<conditions>
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Rewrite" value="max-age=31536000" />
</rule>
</outboundRules>
</rewrite>
I am not sure what flexibility can be implemented but I want this section to work as-is EXCEPT for a specific URL. Specifically, I want all HTTP traffic to continue to be redirect from HTTP to HTTPS EXCEPT when the request is HTTP://{HOST}/symphony/default.aspx.
Is this possible?
You can try adding a condition to the rule to match the request HTTP://{HOST}/symphony/default.aspx, and then use the negate attribute to negate the result of matching.
More information about negate you can refer to this link: Rule pattern properties.

Redirect url with a hashcode to another with same hashcode in IIS10

I need to redirect a url with a variable hashcode to another url with that same hashcode in IIS10 (Windows Server 2019).
Example:
https://www.example.com/hello/sd54effg1g5s11d5111dwds21fds2f1ffd
Needs to redirect to:
https://subdomain.example.com/hello/sd54effg1g5s11d5111dwds21fds2f1ffd
At the moment i have this as a rule in the web.config:
<rule name="rulename" stopProcessing="true">
<match url="^hello/[a-zA-Z0-9]+$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(https:\/\/www\.)example.com$" />
</conditions>
<action type="Redirect" url="https://subdomain.{HTTP_HOST}/{R:1}" />
</rule>
Firstly, the {HTTP_HOST} is will not match the https part in the url. So you should use ^www.example.com$ instead of ^(https:\/\/www\.)example.com$.
Besides, you should use https://subdomain.example.com/{R:1} instead of the https://subdomain.{HTTP_HOST}/{R:1} to achieve your requirement.
Details ,you could refer to below url rewrite rule:
<rule name="rulename" stopProcessing="true">
<match url="^hello/[a-zA-Z0-9]+$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://subdomain.example.com/{R:0}" />
</rule>

Reverse Proxy with redirect to Sub-URL + URL Parameter

I have an IIS for the site web.test.com on Port 80 and 443 that is used as reverse proxy for another web-server configured on the same server on port 8090. So I'm using the IIS rewrite module and ARR module with the following rule.
<rule name="RULE1" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="web.test.com" />
</conditions>
<action type="Rewrite" url="http://localhost:8090/{R:1}" />
</rule>
I would like to add a redirect (or extended rewrite) that if someone goes directly to web.test.com , the URL is rewritten/redirected to web.test.com/foo/index.html?bar (which technically is localhost:8090/foo/index.html?bar) - so it should go to a file in a sub-url and add a URL parameter to the url.
Does anyone know how to do this. I found a lot of instructions on how to redirect, but never in combination with reverse proxy.
Thank you in advance
You could create a redirect rule then Move up the redirect rule ahead your Reverse proxy rule.
<rule name="Redirect Rule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^(/)?$" />
</conditions>
<action type="Redirect" url="/foo/index.html?bar" redirectType="Temporary" />
</rule>
<rule name="RULE1" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="domain.com" />
</conditions>
<action type="Rewrite" url="http://localhost:8090/{R:1}" />
</rule>
The order should looks like this:

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 />

Simple rule(s) for IIS/Azure web.config file to redirect traffic to non-www HTTPS

My goal is to have rule(s) in IIS/Azure's web.config file to successfully redirect the following client request examples:
HTTPS redirect:
From: example.com
To: https://example.com
No-www & HTTPS redirect:
From: www.example.com
To: https://example.com
No-www, HTTPS redirect & maintain path:
From: www.example.com/examplepage
To: https://example.com/examplepage
Here's what I have so far, which only is achieving point 1 and 2. If a visitor lands directly on a subpage, they aren't redirected to non-www and aren't redirected to HTTPS.
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
<add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
<rule name="Canonical Hostname" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}{REQUEST_URI}" redirectType="Permanent" />
</rule>
I've exhausted multiple search result pages and multiple StackOverflow questions without any luck - they all promise to be able to achieve the third point (redirect to a non-www, HTTPS page and maintain the path) but they haven't worked for me.
Thanks for taking the time to read this!
I discovered the problem, which is partly due to the way I presented my question (sorry, StackOverflowers!).
The issue was the ordering of the rules. I've put the following (new) rules at the top of my rules section, in the following order, for those who suffer this problem in the future. Now, it is redirecting to HTTPS, with no-www, and maintaining the path that was requested by the client.
<rule name="Canonical Hostname" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:2}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>