IIS URL Rewrite with multiple query string - iis-7

Im really new in URL Rewriting and trying to rewrite / redirect multiple query but seems not working. Since this is the search result and comes with different filtering the queries may vary. For example is some search we may have the query of t1=something and in the other we may have t2=somethingelse and sometimes we may combine them like: t1=something&t2=somethingelse
Im using IIS7 with web.config and here is what I have done so far:
This is my example link
www.website.com/search/?t1=first&t2=second
I have tried the following and non of them actually worked:
(1)
<rewrite>
<rules>
<rule name="first" stopProcessing="true">
<match url="search/" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="t1=([0-9a-zA-Z]+)" />
</conditions>
<action type="Redirect" url="search/{C:1}/" appendQueryString="false" />
</rule>
<rule name="second" stopProcessing="true">
<match url="search/" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="t2=([0-9a-zA-Z]+)" />
</conditions>
<action type="Redirect" url="search/{C:1}/" appendQueryString="false" />
</rule>
</rules>
</rewrite>
(2)
<rule name="a" stopProcessing="true">
<match url="search2/" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="t1=([0-9a-zA-Z]+)" />
<add input="{QUERY_STRING}" pattern="t2=([0-9a-zA-Z]+)" />
</conditions>
<action type="Redirect" url="search2/{C:1}/{C:2}" appendQueryString="false" />
</rule>
I would really appreciate any help.
Thanks.

Also, this post might provide the answer -
Tracking Capture Groups Across Conditions setting
EDIT: the example from the link above
Note the trackAllCaptures=true
<rule name="Back-references with trackAllCaptures set to true">
<match url="^article\.aspx" >
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="p1=([0-9]+)" />
<add input="{QUERY_STRING}" pattern="p2=([a-z]+)" />
</conditions>
<action type="Rewrite" url="article.aspx/{C:1}/{C:2}" /> <!-- rewrite action uses back-references to both conditions -->
</rule>

I thought I'd share a link of what I had found -
possible answer here
Also, to separate multiple parameters you can use a pipe operator. For example, something like this:
parm1=(\w+)|parm2=(\w+)
apply target URL with {C:1} and {C:2}
So, URL like this:
yourapp/list.aspx?parm1=abc&parm2=123
will result in back references as follows - {C:1}=abc and {C:2}=123

Related

URL rewrite for Google analytics

I have searched through many S.O. questions and answers, but haven't seen a good solution for this. I have already created and well-established SEO URLs in my web.config file. I was using a rewriteMap to basically turn my product pages into any URL that I needed.
Here is what I have to do this:
<rule name="Redirect Rule" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{RedirectMapName:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="Rewrite Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{RewriteMapName:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
What is being requested now is to add in Google Analytics parameters in order to track campaigns. If I simply add in the parameters, it will of course, give me a 404 error. I am now trying to get these SEO URLs to pick up these parameters, but getting quite frustrated.
If I remove the query string, Google Analytics will not pick it up since it is a client side script. Something like this might be close, but I am not pointing to any specific page.
URL Rewrite with Query String
This is how I remove the querystring:
<rule name="Subject redirect with query" stopProcessing="true">
<match url="^(.*)" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="^utm_source=([^=&]+)&utm_medium=([^=&]+)&utm_campaign=([^=&]+)&?(.*)$" />
</conditions>
<action type="Redirect" url="{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
Summed up, how do I turn this:
https://www.example.com/fancy-url
to this
https://www.example.com/fancy-url?utm_source=Targeted_Email&utm_medium=email&utm_campaign=campaigntag
After much frustration, I was able to figure this out. The query string was okay, but one of the settings was preventing the rest of the rules to be processed. After the rule name, I had stop processing set to true. When I looked at it in IIS, the setting is much more descriptive. It says "Stop processing of subsequent rules". Once I removed that and moved this above my other rules, worked perfectly.
<rule name="Google Query string">
<match url="(.*)$" />
<conditions>
<add input="{QUERY_STRING}" pattern="utm_source=([^=&]+)&utm_medium=([^=&]+)&utm_campaign=([^=&]+)&?(.*)$" />
</conditions>
<action type="Rewrite" url="{R:0}" appendQueryString="false" />
</rule>

IIS7 rewrite rule that forces lower case for specified pages on domain

I am trying to create a IIS7 rewrite rule that forces lower case for specified pages so that I can override case sensitive inbound links to my site to avoid Google duplicate content on urls like /Contact and /contact.
I have found a rule that works well except it includes all pages on the domain and then you can specify pages to exclude.
<rule name="Lower Case Rewrite" enabled="true" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="/ordering/shoppingcart/" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
Would it be possible to invert the rule so that it does not apply the to lower case except on the pages I specify in the rule.
The reason for me wanting to do it like this is the admin, cart and other sections of the site are failing when the rule is implemented and the site is too big to check all the pages and handler calls.
Any help would be great!
<rewrite>
<rules>
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
</rules>
</rewrite>
Like this:
<rule name="Lower Case Rewrite" enabled="true" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{URL}" pattern="/content/" />
<add input="{URL}" pattern="/content2/" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
Group your conditions with MatchAny, then add all patterns where the rule should apply (but leave out the negate attribute).

Url rewriting question

This is my rule set up in web.config:
<rule name="RedirectPopups" stopProcessing="true">
<match url="^webforms/visitor/popup/*" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^([^=&]+)=([^=&]+)$" />
</conditions>
<action type="Redirect" url="jwelery/INEEDTHEPAGEVARIABLEHERE/{C:1}/{C:2}" appendQueryString="false" redirectType="Permanent" />
</rule>
Basically I have my popups in webforms\visitor\popup. I want to write a rule that when any page is request within this popup directory. It gets redirected to some custom url.
For Eg.
If user requests webforms/visitor/popup/HelloWorld.aspx?a=1
He should be redirected to jwelery/HelloWorld/a/1
I just need the solution for what should I write in "INEEDTHEPAGEVARIABLEHERE" in Redirect action. Is there any special variable that I can use? I am using IIS7
Thanks.
Why don't you use
<match url="^webforms/visitor/popup/([a-zA-Z0-9]+).aspx\?([a-zA-Z0-9]+)=([a-zA-Z0-9]+)$" />
<action type="Rewrite" url="jwelery/{R:1}/{R:2}/{R:3}" />
?
To answer your original question, you could use UrlRouting.
( http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx)

Conditional query string redirect

I'm trying to redirect requests that have a query string to a different domain name.
I've got a short url, http://short.url and I want to redirect http://short.url?hello to http://long.url/?hello. So the query string has to be kept by the redirect. To make things more complicated, I would like to rediect http://short.url?hello,hello2 to http://long.url/advanced.aspx/?hello,hello2.
Here is the rule I've got now (only dealing with the first part of my question)
<rewrite>
<rules>
<rule name="test" patternSyntax="ECMAScript" stopProcessing="true">
<match url="~/?\w+" />
<action type="Redirect" url="http://long.url/?{R:0}" redirectType="Found" />
</rule>
</rules>
</rewrite>
However, I am not seeing any redirects. Also, is there a better way to do this? Basically I just want to setup a shortcut to pass queries to a website. These are not meant to be permanent so I'm using redirectType="Found".
In case anyone is looking to do this:
<rules>
<rule name="Basic" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\w*" />
<action type="Redirect" url="http://longurl.com" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="\w+" />
<add input="{QUERY_STRING}" pattern="\," negate="true" />
</conditions>
</rule>
<rule name="Advanced" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\w*" />
<action type="Redirect" url="http://longurl.com/advanced.aspx" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="^\w+(?=\,)" />
</conditions>
</rule>
</rules>

Bidirectional URL Rewriting/Redirecting in IIS7.5

First off, I'd like to apologise for the ludicrous title. I'm not trying to sound cool or clever by using the word 'bidirectional', I just genuinely couldn't think of another way to describe it. Promise.
On to my problem. I have the following in the <system.webserver>/<rewrite>/<rules> section of my Web.config.
<!-- Who We Are -->
<rule name="1A">
<match url="^whoweare\.aspx$" />
<action type="Redirect" url="who-we-are" redirectType="Permanent" />
</rule>
<rule name="1B">
<match url="^who-we-are$" />
<action type="Rewrite" url="whoweare.aspx" />
</rule>
<!-- What We Do -->
<rule name="2A">
<match url="^whatwedo\.aspx$" />
<action type="Redirect" url="what-we-do" redirectType="Permanent" />
</rule>
<rule name="2B">
<match url="^what-we-do$" />
<action type="Rewrite" url="whatwedo.aspx" />
</rule>
Now this works tremendously. Effectively, if you visit the URL http://example.com/whoweare.aspx (which is the actual URL of the page), you'll be 301 redirected to the URL http://example.com/who-we-are (the virtual URL), and if you visit the virtual URL, you'll be rewritten to the actual URL.
This means super sexy URLs without duplication, and it doesn't result in reciprocal rewriting either, so smiles all round.
My question is this: could this be done more elegantly?
It's a little cumbersome having to write out two rules to ensure that one is redirected to the other, and the other is rewritten to the one. Is it possible to write one rule which will achieve the functionality of the above two?
Elegance is a subjective term, I guess there are a couple of ways that would be better, such as using Rewrite extensibility and implement fancy mapping logic, but by far the way I would recommend is using just 2 rules, one for Redirect and one for Rewrite, and with that just leverage Rewrite Maps that will make it a bit more readable (but still painful) to manage them, for example below you would now only need to maintain the maps and never have to deal with rules anymore:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite From Pretty URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URLsToRewrite:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="Redirect To Pretty URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URLsToRedirect:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="URLsToRewrite">
<add key="/who-we-are" value="/whoweare.aspx" />
<add key="/what-we-do" value="/whatwedo.aspx" />
</rewriteMap>
<rewriteMap name="URLsToRedirect">
<add key="/whoweare.aspx" value="/who-we-are" />
<add key="/whatwedo.aspx" value="/what-we-do" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>

Resources