IIS URL Rewrite - Change Beginning of URL - redirect

I would like to replace
https://web1.domain.org/cwweb/LauncherInterface.aspx?host=https://web1.domain.org/
with
https://web2.domain.org/cwweb/LauncherInterface.aspx?host=https://web2.domain.org/
and keep everything else after it.
e.g. "https://web2.domain.org/xxxxx/LauncherInterface.aspx?host=https://web2.domain.org/xxxxx&thisid=zzzz"
This is what I have tried:
<rewrite>
<rules>
<rule name="redirect app" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="web1.domain.org" />
</conditions>
<action type="Redirect" url="https://web2.domain.org/cwweb/LauncherInterface.aspx?host=https://web2.domain.org/{R:1}" />
</rule>
</rules>
</rewrite>
This is what it displays: ""https://web2.domain.org/xxxxx/LauncherInterface.aspx?host=https://web1.domain.org/xxxxx&thisid=zzzz"
The second domain name doesn't change.
I pretty much a noob with this. Any assistance would be appreciated.

You can try this rule:
<rule name="test" stopProcessing="true">
<match url="^cwweb/LauncherInterface.aspx$" />
<conditions>
<add input="{HTTP_HOST}" pattern="web1.domain.org" />
<add input="{QUERY_STRING}" pattern="(.*)web1(\.domain\.org.*)" />
</conditions>
<action type="Redirect" url="https://web2.domain.org/{R:0}?{C:1}web2{C:2}" appendQueryString="false" />
</rule>

Related

How to rediret all requests to another host in IIS

I want to redirect all requests from the old domain to a new domain. For example:
From: oldhost.com/app1/houses?city=springs
To: newhost.com/app1/houses?city=springs
I've tried using both the basic "HTTP Redirect" as far as "URL Rewrite" module with the same results. Redirect only works to a limited extent, as long as there is nothing past the first path segment:
oldhost.com/app1 - works
oldhost.com/app1/houses?city=springs - does not work
Here is the attempted URL Rewrite rule:
<rule name="rule1" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://newhost.com/{C:1}" logRewrittenUrl="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="oldhost.com(.*)" />
</conditions>
</rule>
You can try this rule:
<rule name="test">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^oldhost.com$" />
</conditions>
<action type="Redirect" url="https://newhost.com/app1{R:1}" />
</rule>
Got it to work with the following rule:
<rule name="my_redirect" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://newhost.com/{R:0}" logRewrittenUrl="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="oldhost.com" />
</conditions>
</rule>

Syntax in IIS to use re-write subdomain as path of {HTTP_HOST}

Suppose I have a site: https://sub.domain.example, what is the proper syntax to get this to become https://domain.example/sub
The need is regardless of the sub and the Toplevel domain (e.g., .com,.net,.org) to have the redirect push the user to https://domain.example/sub
Here is what I have tried but no go--
<rules>
<rule name="SubDomain to Subdirectory">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(\w+)\.(.*)$" />
</conditions>
<action type="Rewrite" url="https://domain.example/{C:1}" />
</rule>
</rules>
You could try the below rule to achieve your requiremnet:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="on" />
<add input="{HTTP_HOST}" pattern="([_0-9a-z-]+)\.(.*)" />
</conditions>
<action type="Redirect" url="https://domain.example/{C:1}" />
</rule>

How to redirect a IIS url with a query string

I am looking for redirecting a url in IIS from www.abc.com/mysurvey.aspx?mysurveyid=100 to www.abc.com/mysurvey.aspx?mysurveyid=101
Could you please tell me how do I achieve this?
You could use this below rule to implement your retirement:
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="/mysurvey.aspx" />
<add input="{QUERY_STRING}" pattern="mysurveyid=100" />
</conditions>
<action type="Redirect" url="http://www.test.com/mysurvey.aspx?mysurveyid=101" appendQueryString="false" />
</rule>
</rules>
</rewrite>

Two redirections instead of one for Lower Case

When I add the following rule to the redirection file:
<rule name="To Lowercase" enabled="true" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/Images/" negate="true" />
<add input="{REQUEST_METHOD}" pattern="GET" ignoreCase="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
<rule name="Redirect to WWW">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^foo.com$" />
</conditions>
<action type="Redirect" url="http://www.foo.com/{R:0}" redirectType="Permanent" />
</rule>
And attempt to open webiste "Foo.com" watching network behaviour I can see that it's first redirected (301) from "https://Foo.com" to "http://foo.com" and then from "http://foo.com" back to "https://foo.com" with status 302.
Is there a way to modify this rule to make just one redirection?
Based on your answers in comments, you can just simply specify the domain name in your rules to avoid multiple redirects. For example:
<rule name="To Lowercase" enabled="true" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/Images/" negate="true" />
<add input="{REQUEST_METHOD}" pattern="GET" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.foo.com/{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
<rule name="Redirect to WWW">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^foo.com$" />
</conditions>
<action type="Redirect" url="https://www.foo.com/{R:0}" redirectType="Permanent" />
</rule>

Re-write old url to new url Asp.Net

I have 2 domains pointing to the same Umbraco application:
oldexample.com
newexample.com
I want my application to change the URL from:
oldexample.com/...
TO
newexample.com/...
when people visit the oldexample.com.
In my web.config, I've placed this, but to no effect:
<rewrite>
<rules>
<rule name="Redirect old-domain to new-domain" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^oldexample.com$" />
</conditions>
<action type="Redirect" url="http://www.newexample.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\." />
<add input="{HTTP_HOST}" negate="true" pattern="localhost" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
What am I doing wrong?
// The application is hosted in Azure web sites. And both URLs are hostnames assigned to the site.
<rewrite>
<rules>
<rule name="Redirect oldexample.com to newexample.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.oldexample\.com$" />
<add input="{HTTP_HOST}" pattern="^oldexample\.com$" />
</conditions>
<action type="Redirect" url="http://www.newexample.com/{R:1}" />
</rule>
</rules>
</rewrite>