URLrewriteFilter tuckey redirect - tuckey-urlrewrite-filter

I am trying to use UrlRewriteFilter to do a redirect. As we are moving servers and infrastructure all the time to develop our in house cloud solutions we plan to use a lot of this.
My problem is I have a URL like
https:// google.com/web-app/servlet?request-parameters&values
I need to redirect all the URLs which has a value for a particular parameter to a different server.
so a URL like :
https:// google.com/administrator/servlet/ej.processOrder?sec=100&geo=appointment&cus=dubai
If the parameters contain cus=dubai, I need to redirect it to
https://hotmail.com/servlet/ej.processOrder?sec=100&geo=appointment&cus=dubai
I have tried the following but it doesn't seem to work
<urlrewrite use-query-string="true">
<rule match-type="wildcard">
<name>Redirect requests to cases</name>
<description></description>
<from>/administrator/servlet/**?**</from>
<to type="redirect" last="true">http://hotmail.com:8080/servlet/$1?&cus=dubai&$2&$3&amp</to>
</rule>
</urlrewrite>
Thanks for the help.

You will need to make use of the query-string and make sure it matches a particular condition.
From what I can see you would want something like this :
<condition name="query-string" operator="equal">*cus=dubai*</condition>
You will need a rule for each different parameter. One full rule would look like this :
<urlrewrite use-query-string="true">
<rule match-type="wildcard">
<name>Cus = dubai</name>
<from>/administrator/servlet/*</from>
<condition name="query-string" operator="equal">*cus=dubai*</condition>
<to type="redirect" last="true">https://hotmail.com/servlet/ej.processOrder?sec=100&geo=appointment&cus=dubai</to>
</rule>
<!-- other rules go here -->
</urlrewrite>

The condition shall be "type" instead of "name", as:
<condition type="query-string" operator="equal">*cus=dubai*</condition>

Good question and for a situation that I imagine occurs often it is not very well documented in the manual in my opinion.
The following worked for me -
<rule>
<condition type="query-string">foo=123&bar=456</condition>
<from>/index.jsp</from>
<to type="permanent-redirect" last="true">https://somehwere-else.com/blah/blah/</to>
</rule>
So this should work for you -
<rule>
<name>Redirect requests to cases</name>
<condition type="query-string">sec=100&geo=appointment&cus=dubai</condition>
<from>/administrator/servlet</from>
<to type="permanent-redirect" last="true">http://hotmail.com:8080/servlet/sec=100&geo=appointment&cus=dubai</to>
</rule>

Related

How to make a rewrite rule in IIS when a querystring parameter is missing?

I want to redirect when a Url is missing a part of the querystring. So, for example
https://www.example.com?foo=1&bar=2
is a valid url, don't do anything. But, when the url looks like this:
https://www.example.com?foo=1 I want to redirect to https://www.example.com. 'bar' is missing here, but 'bar' can be any name.
For this I want to use IIS redirects using regex. I've found this question: IIS URL Rewrite - if query parameter(s) is missing but I can't get it this to work in my scenario. This is because every other querystring parameter after the ? can have any name. The first parameter is always foo, but the rest of the parameters can have any name.
Can someone help me with the correct regex for this?
Edit:
Real examples of querystrings that must be redirected:
?crop=0,0,0,0 or
?crop=2.6111111111111112,0.22222222222222221,0,0
Both lacks the & so they must be redirected.
You could try below url rewrite rule:
<rule name="query string rule" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll">
<add input="{QUERY_STRING}" pattern="^foo=(\d{0,5})$" />
</conditions>
<action type="Redirect" url="http://www.sample1.com/" appendQueryString="false" />
</rule>

Proper way to fix this rewrite rule?

We have an application that is run in a virtual folder in IIS. We don't want the virtual folder name to be part our links though (primarily to preserve original link names for SEO reasons).
So here is one example of a rewrite rule we're using:
<rule name="Rewrite Account controller to UI">
<match url="/Account(.*)"/>
<action type="Rewrite" url="ui/Account{R:1}"/>
<conditions>
<add input="{URL}" pattern="\.axd$" negate="true" ignoreCase="true"/>
</conditions>
</rule>
The problem with this rule is that it would also match "~/someothercontroller/258642/Accounting-Essentials" and turn it into "/ui/Accounting-Essentials". And I don't want to include the host because the host is different in each environment.
What would this need to look like to match only if the expression is the first thing after the host?
Edit:
Sorry, I guess my post wasn't as clear as I thought it was. An example would be http://x/Account. This should rewrite to http://x/ui/Account. The x could be any host name with any number of periods but it's only the host name so it wouldn't contain any slashes.
You can see in the rule I have above that I want it to include anything that comes after Account however I realize that's not quite right either because it shouldn't match "http://x/Accounting", but it should match "http://x/Account/whatever".
So essentially, you want to make sure that Account comes right after the host, and also that Account is the full name of the directory. You can achieve this like so:
<rule name="Rewrite Account controller to UI">
<match url="^Account(/.*)?"/>
<action type="Rewrite" url="ui/Account{R:1}"/>
<conditions>
<add input="{URL}" pattern="\.axd$" negate="true" ignoreCase="true"/>
</conditions>
</rule>
The ^ ensures that this is the beginning of the string that you are evaluating.
The / after Account ensures that you only rewrite the url if "Account" is the full name of the directory.
It appears from the documentation that the inital / will not be included in the string you're evaluating (which is why I removed it), but you can test it both ways to be sure.
Also note that I added a / before {R:1}.
Edit: Another way
You could also add a rule that verifies that the whole URL matches a certain pattern. This might actually be an easier way:
<rule name="Rewrite Account controller to UI">
<match url="/Account(.*)"/>
<action type="Rewrite" url="ui/Account{R:1}"/>
<conditions>
<add input="{URL}" pattern="\.axd$" negate="true" ignoreCase="true"/>
<add input="{REQUEST_URI}" pattern="^/Account(/.*)?" ignoreCase="true"/>
</conditions>
</rule>
The Microsoft docs give this example of the server variable values:
For example, if a request was made for this URL:
http://www.example.com/content/default.aspx?tabid=2&subtabid=3, and a rewrite rule was defined on the site level then:
The rule pattern gets the URL string content/default.aspx as an input.
The QUERY_STRING server variable contains tabid=2&subtabid=3.
The HTTP_HOST server variable contains www.example.com.
The SERVER_PORT server variable contains 80.
The SERVER_PORT_SECURE server variable contains 0 and HTTPS contains OFF.
The REQUEST_URI server variable contains /content/default.aspx?tabid=2&subtabid=3.
The PATH_INFO server variable contains /content/default.aspx.

IIS Url Rewrite with Capture Groups for First Item In List

Our application depends on the client IP address for various features. We've updated our infrastructure and changed how we load balance our application. Because of the change, the CGI.REMOTE_ADDR being reported to the application is that static load balancer IP not the client IP. To fix this, the LB support said to add a rule to both the LB and IIS. The LB is now configured to send a header [http_x_forwarded_for] with the client ip.
The rewrite rule in IIS looks like this:
<rule name="REMOTE_ADDR rewrite" enabled="true" patternSyntax="ECMAScript">
<match url="^((?!GetImage).)*$" />
<serverVariables>
<set name="REMOTE_ADDR" value="{HTTP_X_FORWARDED_FOR}" />
</serverVariables>
<action type="Rewrite" url="{R:0}" appendQueryString="true" logRewrittenUrl="false" />
</rule>
<rule name="REMOTE_ADDR blank URL rewrite" enabled="true">
<match url="^$" />
<serverVariables>
<set name="REMOTE_ADDR" value="{HTTP_X_FORWARDED_FOR}" />
</serverVariables>
<action type="Rewrite" url="default.cfm" />
</rule>
The first rule is for any pages requested excluding the GetImage page. The second is for no page.
For the most part this works correctly. We haven't had to make any adjustments to the application because the CGI.REMOTE_ADDR is correct.
But we were told that in some circumstances the LB may pass multiple IP addresses and to use only the first one in the list. This is causing a problem.
In some cases CGI.REMOTE_ADDR is coming through like this '100.200.200.200, 123,123,123,123'.
Testing the RegEx in IIS with that string shows group {R:0} is 100.200.200.200, 123,123,123,123.
The question is is it possible to write the rule so that {R:0} is the first matched IP address in the list?
Realized after I posted this I was attacking it (or asking..) the wrong way.
The URL Rule regex is to match the URL to apply the rule. All requested URLS that match will map the http_x_forwarded_for header value into the REMOTE_ADDR server variable. The value of this variable which could be invalid what the problem.
To fix this, a new OUTBOUND rule needed to be added. In this rule you can specify the REMOTE_ADDR as the INPUT and matching against a regex to rewrite it. By adding the rule, a regex to match the first IP addr found in the variable, and then rewrite the variable with the back trace I was able to solve the problem.
<outboundRules>
<rule name="Format IP Address">
<match serverVariable="REMOTE_ADDR" pattern="^([0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3})(.)*$" />
<action type="Rewrite" value="{R;1}" />
</rule>
</outboundRules>

Rewrite Rule in IIS (web.config) - Negate All Subfolders

I need to rewrite a certain page on my website from an extension-based page (.asp) to a non-extension based page. However when I do this the rule also affect all sub-folders. The page is as follows:
www.mysite.com/my-page
It needs to re-write to:
www.mysite.com/my-page.asp
However I need to negate the following from re-writing:
www.mysite.com/my-page/sub-folder-1
www.mysite.com/my-page/sub-folder-1/sub-folder-2
etc.
The code I currently have is as follows:
<rule name="re-write-rule-test" stopProcessing="true">
<match url="^my-page" />
<action type="Rewrite" url="/my-page.asp" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/([^/]+)?$" negate="true" />
</conditions>
</rule>
But it isn't working. The re-write on the actual page works fine but the the negation is failing and all sub-folder pages are also being re-written.
Can you please help?
Thanks,
Jason
Answered by Tensibai as per the below. Thanks!
I assume changing by would be enought (adding a termination to the match to avoid matching everything starting by 'my-page'. – Tensibai 58 mins ago

IIS 7.0 rewrite module for file name

I have a website with many links to *.index.html pages that do not exist any more, all default documents have now changed to index.htm, I'm trying to set a URL redirect/rewrite to the new pages but with no luck, here is an exmaple:
http://example.com/somedirectory/maybeanotherdirectory/index.html
I need to redirect that request to:
http://example.com/somedirectory/maybeanotherdirectory/index.htm
I have added different rules to the web.config but so far I can only redirect to:
http://example.com/index.htm
How do I sustain the exact path while only changing index.html to index.htm?
Such an old question, and I stumbled across it purely by happenstance. In case anybody comes across it in look for an answer, you can install the IIS URL ReWrite module, and add the following to your web.config:
<rewrite>
<rules>
<rule name="Html to htm">
<match url="(.*)\.html" />
<action type="Rewrite" url="{R:1}.htm" />
</rule>
</rules>
</rewrite>
Cheers!