IIS URL Rewrite module unencoding query string - iis-7.5

We have a really simple URL rewrite rule that basically causes a 301 redirect to another domain:
<rewrite>
<rule>
<match url="^(default.aspx)?$" />
<action type="Redirect" url="https://some.other.domain.com" />
</rule>
</rewrite>
Unfortunately, when a URL like this is encountered:
http://original.domain.com/?returnUrl=https%3A%2F%2Fsome.url%2F
The redirect URL looks like this:
https://some.other.domain.com/?returnUrl=https://some.url/
Notice how the URL encoding is lost.
Is this a bug in the URL rewrite module? If so, how can one work around it?

I figured it out. The rewrite rule has only a partial redirect URL specified. Query string encoding is now preserved now that I changed this:
<action type="Redirect" url="https://some.other.domain.com"/>
...to this:
<action type="Redirect" url="https://some.other.domain.com/"/>
(note the trailing slash)

Related

IIS rewrite rule to redirect when {REQUEST_URI} is not empty

I need to redirect when REQUEST_URI is not empty. For example, for a incoming request like:
https://www.mydomain
I need to stay to:
https://www.mydomain
However for a request like:
https://www.mydomain/products/customized
I need to redirect to:
https://www.otherdomain/products/customized
How should I write this rule?
According to your description, I suggest you could try to use below url rewrite rule to achieve you requirement.
<rule name="Test Rule" stopProcessing="true">
<match url=".+" />
<action type="Redirect" url="http://otherdomain.com/{R:0}" />
</rule>
Result:
Has the request url

IIS Rewrite everything after the domain

I have to domains - a.com and b.com. I want to use iis rewrite so that anything going to www.a.com/ will be redirected to www.b.com/index.asp?i=.
For example www.a.com/XXX will be redirected to www.b.com/index.asp?i=XXX
I put (.*?) in the pattern. The problem is when I enter http://www.b.com/index.asp?i={REQUEST_URI} as a redirect action, the redirect url includes a slash. For example www.a.com/XXX is redirected to www.b.com/index,asp?i=/XXX instead of to i=XXX. Is there any way to get rid of the slash?
You could use below url rewrite rule:
<rule name="test1" enabled="true" stopProcessing="true">
<match url=".+" />
<conditions>
</conditions>
<action type="Redirect" url="http://www.b.com/index.asp?i={R:0}" />
Regards,
Jalpa

URL Rewriting IIS 7 Querystring Issue

I'm running a legacy website which has a tonne of 301 redirects on it. In an effort to make my life simpler, I switched them from doing manual 301 redirects in IIS to using the url rewriting module and a rewrite map, which I can generate from the database.
Unfortunately I've now hit a snag, in that some of their urls come with querystrings (from Google's adwords), and these invariably 404 (because they don't match the urls in my rewrite map).
Is there a way around this, or do I have to go back to manually doing the 301s?
For the record: we're using IIS7
Code samples:
url: http://mydomain.com/widgets/?gclid=[google code]
Rewrite Rule:
<rule name="Rewrite rule1 for News301Redirects" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{News301Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{C:1}" appendQueryString="false" />
</rule>
Map Line:
<add key="/widgets/" value="/widgets.asp" />
Well, the answer proved to be depressingly simple - just an absolute pig to find.
Instead of using REQUEST_URI (which gets the whole requested URL, including the querystring) I've switched to using URL (which just gets the URL without the querystring) and that seems to have utterly resolved my problems.

IIS rewrite module 301 redirects remove folder adding extra slash

I have this IIS rewrite rule
<rule name="Redirect rule for aliased Pages">
<match url="^/pages/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
The idea is that it should redirect http://mydomain.com/pages/about-us/ to http://mydomain.com/about-us/. According to the tester in IIS in that case R:1 is about-us/ but the redirect always goes to http://mydomain.com//about-us/ (see double //). I have tried removing both forward slashes from the match URL and leaving each one in on its own and it does not seem to make any difference.
Any idea where that extra / is coming from?
The URL rewrite module starts checking after the initial trailing slash, so the following would remove the first level folder "pages":
<rule name="Redirect rule for aliased Pages">
<match url="^pages/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
Testing with:
http://example.com/pages/about-us/ :: 301: http://example.com/about-us/
http://example.com/pages/advertising/ :: 301: http://example.com/advertising/
As expected.
Note that if you have the rewrite rules in a separate file, you will need to recycle the app pool or restart IIS for the changes to work.
Also, order of the rules is important too.

IIS URL Rewrite root only

I have 2 URLs
http://test.mysite.com (1) and
http://test.mysite.com/app (2)
I would like to redirect requests to (1) to www.othersite.com
when (2) shuld not be redirected.
Is it possible to do with url rewrite using regex?
br
Yes, it is possible with URL rewrite module. Your rule looks like that:
<rule name="Redirect non app" stopProcessing="true">
<match url="^app(.*)" negate="true" />
<action type="Redirect" url="http://www.othersite.com{REQUEST_URI}" />
</rule>

Resources