URL Rewrite back reference action URL not working - iis

I have setup a url rewrite rule to redirect from one domain to another in case matching the condition. i have a site abc.aaa.com which should redirect to abc.bbb.com if url matches with *.aaa.com. When I hard code the action URL its working fine but using back reference its not working.
I am using IIS 8.5
Below are the rules.
This is not working. When I am doing this URL is showing http://abc.aaa.com/abc.bbb.com
<rule name="Redirect aaa.com" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).aaa.com(.*)$" />
</conditions>
<action type="Redirect" url="{C:1}.bbb.com{C:2}" appendQueryString="false" />
</rule>
This is working
<rule name="Redirect aaa.com" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).aaa.com(.*)$" />
</conditions>
<action type="Redirect" url="http://abc.bbb.com" appendQueryString="false" />
</rule>
I have tried the same pattern in rule pattern and same action without condition. thats also not working
<rule name="Redirect aaa.com" enabled="true" stopProcessing="true">
<match url="(.*).aaa.com(.*)$" />
<action type="Redirect" url="http://abc.bbb.com" appendQueryString="false" />
</rule>

I have found that {C:1} doesn't contain http:// part and seperated that in URL action . it's working fine.Below is the modified rule
Thanks Lex Li for the quick response
<rule name="Redirect aaa.com" enabled="true" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).aaa.com(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:1}.bbb.com{PATH_INFO}" appendQueryString="true" />
</rule>

Related

Redirect from http to https on IIS 10.0

I have an aspx web app accessed via http://domain/web.aspx. This web app uses port 80 and http://domain:80/web.aspx works OK. I would like to redirect all calls to https://domain:82/web.aspx. I've tried using the rewrite rule
<rule name="HTTPS force" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(.*):80$" />
</conditions>
<action type="Redirect" url="https://localhost:82" appendQueryString="false" />
</rule>
in web.config in the same folder as web.aspx but this has no effect. What rewrite rule do I need to use?
You should use the following as input:
<rule name="HTTPS force" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
</rule>
Not sure if you want the :80 there in the URL? If so, it can be added to my example too. Taken from here: Web.config redirects with rewrite rules - https, www, and more.
Found this rule worked
<rule name="HTTPS force" enabled = "true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="http://domain/Web.aspx" />
</conditions>
<action type="Redirect" url="https://domain:82/Web.aspx"/>
</rule>

iis rewrite url for https://example.com/default.aspx/default.aspx to https://example.com

I have tried all kinds of rewrite URL's and can't get IIS to send the URL of https://example.com/default.aspx/default.aspx to https://example.com/default.aspx
Google has indexed the site with the wrong URL when Bing got it right (go figure). Any help would be much appreciated. All my traffic is going to the wrong url (https://example.com/default.aspx/default.aspx).
<rewrite>
<rules>
<rule name="Redirect www.xxx.com to xxx.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^https://example.com/default.aspx/default.aspx" />
</conditions>
<action type="Redirect" url="default.aspx" />
</rule>
<rule name="redirect two character to default" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/[a-z]{2}(/)?$" />
</conditions>
<action type="Redirect" url="default.aspx" appendQueryString="false" />
</rule>
</rules>
</rewrite>
There is some issue in your rule. {HTTP_HOST} only matches the hostname which is www.example.com it will not match the whole URL.
You could try below rule:
<rule name="Redirect www.xxx.com to xxx.com" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="default/default|default.aspx/default.aspx" />
</conditions>
<action type="Redirect" url="default.aspx" />
</rule>

Rewrite domain to specific language content without changing url - IIS

I have multiple domain names pointed to the same server (example.nl)
The example.my url should point to the content of example.nl/en, without having this url to appear in the adress bar. This is only really necessary for the homepage (due to google addwords issues).
To summarize, example.my should show the content of example.nl/en while showing example.my in the adress bar.
The current rule is giving me an error. Any tips?
<rule name="Redirect .my" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^.*example\.my$" />
</conditions>
<action type="Rewrite" url="http://www.example.nl/en/" />
</rule>
This seems to do the trick for me:
<rule name="redirect .my to /en" enabled="true" stopProcessing="true">
<match url="(^$)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.my" />
</conditions>
<action type="Redirect" url="http://www.example.my/en/{R:1}" appendQueryString="true" />
</rule>

IIS URL Redirect change querystring parameter name

I am trying to redirect from one site to another like the following.
http://example.com/quickscan/?code=123456
To
https://example2.com/as-quickscan/login?username=123456
The domains are different. Also the redirect URL parameter and URL structure is entirely different.
What I have tried is the following and some other variations.
<rule name="Redirect quickscan" stopProcessing="true">
<match url="^quickscan/\?[a-zA-Z]+=([0-9]+)?$" ignoreCase="true" />
<action type="Redirect" url="https://example2.com/as-quickscan/login?username={R:1}" appendQueryString="false"/>
</rule>
<rule name="Redirect quickscan" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="^quickscan/\?[a-zA-Z]+?=([0-9]+)?$" />
</conditions>
<action type="Redirect" url="https://example2.com/as-quickscan/login?username={R:1}" redirectType="Permanent" appendQueryString="false" />
</rule>
This is fixed. I used the following.
<rule name="Redirect quickscan" stopProcessing="true">
<match url="^quickscan/" ignoreCase="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="([a-z]+)=([0-9]+)" />
</conditions>
<action type="Redirect" url="https://example2.com/as-quickscan/login?username={C:2}" appendQueryString="false" />
</rule>

IIS 7 URL Rewrite match for particular URL

I would like to match exactly https://internet.dev.local/KYR url (without / into end) and redirect or rewrite to https://internet.dev.local/KYR/ (with /).
I am trying the following rule but it matches other URLs as well e.g. https://internet.dev.local/KYR/Admin/Form/Default.aspx?signup=false, which is wrong.
so how can I achieve this?
<rule name="Static redirect" patternSyntax="ExactMatch" stopProcessing="true">
<match url="https://internet.dev.local/KYR" negate="true" />
<conditions>
</conditions>
<action type="Redirect" url="/Login/?ReturnUrl=/Member/KYR/" redirectType="Permanent" />
</rule>
If you need to test the host and protocol you have to put it in the conditions, not in the global rule.
Following your example, it would be as follow:
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url="KYR" />
<action type="Redirect" url="/KYR/" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="internet.dev.local" />
<add input="{HTTPS}" pattern="on" />
</conditions>
</rule>
I have changed the url in the action because your question says:
redirect or rewrite to https://internet.dev.local/KYR/ (with /).
EDIT:
To get it to work on any host (with or without SSL), just remove the conditions:
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url="KYR" />
<action type="Redirect" url="/KYR/" redirectType="Permanent" />
</rule>

Resources