IIS rewrite rule not passing query parameter - iis

I think I am trying to do something that should be simple. I want to have a rewrite rule that takes any incoming request, from many domains, and rewrites it to /redirect.aspx?src=original URL
This is what I have
<rewrite>
<rules>
<rule name="SimpleURL" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" negate="true" pattern="\.aspx$"/>
</conditions>
<action type="Redirect" url="/Redirect.aspx?src={R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
It works except the query parameter being passed is src= without any value.
I made a change so it's now passing a hard coded value for the query parameter
I made a change so it is now passing a value for the parameter.
<rewrite>
<rules>
<rule name="SimpleURL" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" negate="true" pattern="\.aspx$"/>
<add input="{QUERY_STRING}" pattern="src=(.*)" negate="true" />
</conditions>
<action type="Redirect" url="/Redirect.aspx?src=foobar" appendQueryString="true" redirectType="Found" />
</rule>
</rules>
</rewrite>
If I replace the foobar with {R:0}, no value is passed. enter code hereThe hard coded foobar value is passed.

Your rule looks fine, and works fine on my machine however you are using a permanent redirect. Did you make any changes to the rule while testing? Try testing it from an Incognito/Private mode browser or test a different URL.
Try the following rule:
<rule name="SimpleURL" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{URL}" negate="true" pattern="\.aspx$"/>
<add input="{QUERY_STRING}" pattern="src=(.*)" negate="true" />
<add input="{URL}" pattern="(.*)" />
</conditions>
<action type="Redirect" url="/Redirect.aspx?src={C:2}" appendQueryString="true" redirectType="Found" />
</rule>

Related

i am trying to rewrite url for home page and category in webconfig c#

<rule name="Default" stopProcessing="true">
<match url="^(.*)$"/>
<conditions>
<add input="{URL}" negate="true" pattern="\." ignoreCase="false"/>
</conditions>
<action type="Rewrite" url="Default.aspx?Alias={R:1}"/>
</rule>
<rule name="Categorys" stopProcessing="true">
<match url="^(.*)$"/>
<conditions>
<add input="{URL}" negate="true" pattern="\." ignoreCase="false"/>
</conditions>
<action type="Rewrite" url="/Vn/Categorys.aspx?Alias={R:1}"/>
</rule>
i want URL:
https://domain/ ==> Default
https://domain/Categorys ==> Categorys
but it only goes to the homepage
can you help me fix it?
From the above description, it looks like your requirement is to redirect from https://domain/Category to https://domain/Vn/Categorys.aspx?Alias=123
I could see that your rules does not has correct patterns.
I would suggest you try to make a test with the rule below.
<rewrite>
<rules>
<rule name="rule-4" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="/Vn/Categorys.aspx?Alias={C:1}" appendQueryString="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="/category/(.*)" />
</conditions>
</rule>
</rules>
</rewrite>
Output:
Further, you could modify the rule as per your own requirements.

Rewriting subdomains to subfolders and parameter in IIS

I'm trying to wrap my head around this problem;
I have a default site with an application that resides in a subfolder, e.g. /app/app.html
It also accepts a language parameter, so for example http://192.168.0.1/app/app.html?language=fi would work.
Now I have two subdomains that I need to not only get rewritten to the correct folder, but also include the language parameter. For example:
fi.domain.com -> http://1.1.1.1/app/app.html?language=fi
swe.domain.com -> http://1.1.1.1/app/app.html?language=swe
I've made A records for both subdomains to point to 1.1.1.1
Currently there are no special bindings (only port 80, no hostnames and all IPs) and no special default pages.
EDIT: I've tried using the URL rewriter module, but I haven't been able to get it work as intended.
EDIT 2: My first example of how I need it to work was a bit flawed, here's a better version;
finnishword.domain.com -> http://1.1.1.1/app/app.html?language=fi
otherwordinswedish.domain.com -> http://1.1.1.1/app/app.html?language=swe
I'm not sure to well understand your question.
It seems that you need URL rewriting rules.
According to this link
<rule name="CName to URL - Rewrite" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.domain\.com$" />
</conditions>
<action type="Rewrite" url="/app/app.html?language={C:1}" />
</rule>
I think it's what you need ;)
Edit 24/08/2016
If you can't have a dynmique pattern with RegEx you have to do as many rules as you have subdomains:
<rule name="finnishRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(finnishword)\.domain\.com$" />
</conditions>
<action type="Rewrite" url="/app/app.html?language=fi" />
</rule>
<rule name="finnishRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(otherwordinswedish)\.domain\.com$" />
</conditions>
<action type="Rewrite" url="/app/app.html?language=swe" />
</rule>
or you can mixup all together if you want to redirect all subdomains that contains the word "swedish" on url with ?
<rule name="finnishRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*swedish.*)\.domain\.com$" />
</conditions>
<action type="Rewrite" url="/app/app.html?language=swe" />
</rule>
So after all the RegEx pattern is up to you ;)
Edit 25/08/2016
Maybe you have to add condition to skip static files
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
...
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<rules>
<clear />
<rule name="swedishMainRule" enabled="true" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?swedishword\.domain\.fi$" />
<add input="{QUERY_STRING}" pattern="language=" negate="true" />
</conditions>
<action type="Redirect" url="?language=sve" appendQueryString="false" logRewrittenUrl="false" />
</rule>
<rule name="swedishRule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?swedishword\.domain\.fi$" />
</conditions>
<action type="Rewrite" url="/app/{R:1}" />
</rule>
<rule name="finnishRule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?anotherfinnishword\.domain\.fi$" />
</conditions>
<action type="Rewrite" url="/app/{R:1}" />
</rule>
</rules>
This is how I ended up doing it, basically the first rule deal with the language parameter, and the other rules just rewrite the URL. The Finnish rule doesn't need an extra language rule since the default language of the app is Finnish.

URL rewrite between 2 urls that are being handled by the same web.config

I am getting a redirect loop when I try and implement this mobile redirect I created:
<rewrite>
<rules>
<rule name="Mobile Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="website.com.au" />
<add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
</conditions>
<action type="Redirect" url="http://mwebsite.com.au" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
This is the catch though, the mwebsite.com.au is assigned to the same web.config as website.com.au so they are handled by the same web.config. This is the way the .net application I am dealing with is handling the request (I cannot split them up, they have to pass through this 1 web.config)
I have tested this by replacing mwebsite.com.au with google.com.au and it works perfectly but for some reason URLREWRITE cannot process the request when it has to inject mwebsite.com.au back through the same rule.
any help would be amazing.
Your rule is basically saying: if {HTTP_HOST} contains website.com.au and {HTTP_USER_AGENT} contains any of midp, mobile or phone, redirect to http://mwebsite.com.au.
As you can guess, http://mwebsite.com.au contains website.com.au.
To fix this, simply tell your condition that it should START with website.com.au using the pattern ^website.com.au.
So your rule would become:
<rewrite>
<rules>
<rule name="Mobile Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^website.com.au" />
<add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
</conditions>
<action type="Redirect" url="http://mwebsite.com.au" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

Canonical Redirect on the server only

I can redirect all domain.com-s on a www.domain.com and it's working on the server like a charm:
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But is has one side effect - it is also working when I am on a testing environment, like on localhost and it always redirects on a production server.
How is it possible to add some condition that is would work on the server only?
This can be achieved using a second condition requesting the host to be a domain.com sub-domain:
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain\.com$" />
<add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>
</rules>
</rewrite>
(note that you were missing a \ in your condition and that redirectType="Permanent" is optional)

Redirect all naked domain urls to subdomain(www) urls preserving the url, except for one page on IIS/ASP.NET

Whats the best way to achieve the above? I do know that it can be achieved at HttpModule level. Is it possible just via web.config(easier and faster to code execute).
It's easy to do this with the URL rewrite module through the web.config :
<rewrite>
<rules>
<clear />
<rule name="Redirect naked domains to www.domain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" negate="true" pattern="^www\." />
<add input="{REQUEST_URI}" negate="true" pattern="^noredirect/forthis/page\.aspx$" />
<add input="{REQUEST_URI}" negate="true" pattern="^noredirect/forthis/page-as-well\.aspx$" />
<add input="{REQUEST_URI}" negate="true" pattern="^noredirect/forthis/page-as-well-too\.aspx$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Or if you really only have a single page that doesn't need to be redirected, it can be even shortened to:
<rewrite>
<rules>
<clear />
<rule name="Redirect naked domains to www.domain.com" stopProcessing="true">
<match url="^noredirect/forthis/page\.aspx$" negate="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" negate="true" pattern="^www\." />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

Resources