IIS redirection rules using query string parameters - redirect

I want to make a not-so-common redirection on IIS and I'm not too sure this is possible.
In my case I want to do something like that:
http://www.mywebsite/app/stuff?id=123 to http://www.mywebsite/app2/stuff?id2=456
Since there is nothing in common between the 2 parameters (only a file mapping the old URL and the new one) I was planning to create manual redirection rules for each entries to be redirected.
However my first simple tests showed by that it doesn't seem to work.
Is what I'm trying to achieve even possible? If it is, what am I doing wrong?
Thanks in advance.

According to your description, if you want to modify the querystring by using url rewrite, I suggest you could try to use below rule:
<rule name="MatchQueryString" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=123" />
</conditions>
<action type="Redirect" url="/app2/stuff?id2=456" appendQueryString="false" />
</rule>

Related

IIS URL Redirect with input parameters

Having trouble implementing my first IIS redirect.
I have a URL:
domain.com/Q7WebServer/Q7WebSrv.exe/datasnap/rest
It can accept a range of input parameters separated by / characters:
domain.com/Q7WebServer/Q7WebSrv.exe/datasnap/rest/GetDateTime
domain.com/Q7WebServer/Q7WebSrv.exe/datasnap/rest/GetUsageData
etc, etc.
I'd like to redirect to a new module (a DLL in fact):
domain.com/Q7WebServer/Q7WebSrvISAPI.dll/datasnap/rest
I started by adding my redirect to the file itself (Q7WebSrv.exe) and it works fine by itself. It's only when I add variables that it starts behaving in ways I don't understand. For example if I have the redirect set to Q7WebSrvISAPI.dll$V$Q (using the EXACT flag) and request
domain.com/Q7WebServer/Q7WebSrv.exe
I get back
domain.com/Q7WebServer/Q7WebSrvISAPI.dll/Q7WebServer/Q7WebSrv.exe
No matter which $ variables I use, it's still trying to append part of the original URL to the redirect. I don't even want to get into what happens when appending the / input parameters.
Do I need to get into URL rewriting? That's a whole can of worms I'm not wanting to open just yet.
you could try this rule:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.domian.com" />
<add input="{REQUEST_URI}" pattern="/Q7WebServer/Q7WebSrv.exe/datasnap/rest/(.*)" />
</conditions>
<action type="Redirect" url="http://ww.domain.com/Q7WebServer/Q7WebSrvISAPI.dll/datasnap/rest/{C:1}" />
</rule>

iis web.config redirect certain domain/path to a new domain/samepath

I need to use web.config to setup a redirect for a certain path/folder to a new domain using the same original path/folder.
Ex:
If www.domain1.com/path2match1/* is detected then redirect to:
www.newdomain.com/path2match1/* (same path as matched on domain1)
ex:
current: www.domain1.com/path2match1/someFileOrFolders/somefile.html
redirect to: www.newdomain.com/path2match1/someFileOrFolders/somefile.html
Any help would greatly be appreciated
thanks
I managed to find an answer for my question on my own! (Yay for me! haha)
Anyways,
Here's how to do what I was asking:
<rewrite>
<rules>
<rule name="NAME" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="(.*)/FOLDER/(.*)" />
</conditions>
<action type="Redirect" appendQueryString="false" url="http://DESTINATION-URL.TLD/FOLDERS/{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Caution! Adding multiple or tag set or placing this code in the wrong location will cause the site to generate internal server errors, making it unavailable for visitors until the additional tags are removed.
NAME can be anything, as long as it is unique
/FOLDER/ is the folder you are redirecting from
http://DESTINATION-URL.TLD/FOLDERS/ is where you are redirecting to.

IIS Redirects to multiple domains and preserves the request file path and names

I'm running IIS 8, Windows Server 2012.
My website has many sub-directories. An ideal url will look like:
oldsite.com/subdir/subdir/pages/default.aspx
I need to redirect (301) visitors to different domains and need to preserve the rest of the original url's file path (everything from the 2nd to the last slash "/").
I have a map of my redirects in an excel sheet, it looks like below:
oldsite.com/subdirA/subdirB/pages/default.aspx -> newsiteA.com/subdirC/subdirD/pages/default.aspx
So you see the whole thing has to change except the last part.
My web.config looks like this:
<rewrite>
<rewriteMaps>
<rewriteMap name="RedirectsCollectionParshareSites">
<add key="/subdirA/subdirB/" value="/subdirC/subdirD/" />
<!--will have many of these add keys-->
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rule1 for RedirectsCollectionParshareSites">
<match url="^(.+)" />
<conditions>
<add input="{RedirectsCollectionParshareSites:{REQUEST_URI}}" pattern="^([^/]+)/([^/]+)(.*)" />
</conditions>
<action type="Redirect" url="http://newsiteA.com/{C:1}{C:3}" appendQueryString="true" />
</rule>
</rules>
My wish is to have many "add keys" - with the mapping from my excel sheet, and per new domain, I will have a new "rewriteMaps" section, with it's own "action".
My question is, is this the proper way of using this module? Any other suggestions?
Remember, I have about a thousand of these redirects to implement.
Any help would be appreciated.
I wasn't able to use the rewritemaps approach, but was able to do it by doing a rule per site:
<rule name="Rule:tes" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^(\/subdirA)(\/subdirB)(.*)" />
</conditions>
<action type="Redirect" url="http://newsite.com/subdirC/subdirD/{C:3}" />
</rule>
It's harder for admins to update, but it works.

Creating a IIS Url Rewrite/Redirect

Im struggling to get a url rewrite/redirect to work in IIS. I've installed the url rewrite module and all the rules fail to do anything. Here is the scenario, we want all web requests which generate a report to be pushed off to a secondary server so it doesn't harm the main box. The web requests that generate reports look something like this:
http://mywebaddress/api/Actionname=GenerateReport&param=123
So im wanting to do some type of regex check on finding any web requests that have "GenerateReport" in it and redirect it to something like:
http://mywebaddressofsecondserver/api/Actionname=GenerateReport&param=123
Any ideas on how the redirect/rewrite would go for this?
You need to check if REQUEST_URI contains Actionname=GenerateReport.
If so, you'll redirect it to other webserver url equivalent.
Translated to an IIS rewrite rule, it would look like this
<rule name="Delegate report generation" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_URI}" pattern="Actionname=GenerateReport" />
</conditions>
<action type="Redirect" url="http://mywebaddressofsecondserver/{R:1}" />
</rule>
Thanks, Justin Iurman,
Your answer solved my issue of getting methods
http://mywebaddress/api/Param/Action1
http://localserverwithport/api/Param/Action1
but for below Post methods it's still giving 404 not found
http://mywebaddress/api/Param/PostAction2
http://localserverwithport/api/Param/PostAction2
Post parameters are:
{"Param1":"James","Param2":"jani"}
My implementation is
'<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Rule1" >
<match url="^(.*)" />
<action type="Redirect" url="http://localserverwithport/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>'

Redirect rule in web.config

I was looking into this Seemingly simple redirect in IIS using web.config file because I have a similar situation, but the approach mentioned there doesn't seem to be working for me.
I have a help section on a web site, which for now I want it to redirect to another place. The rest of the site should stay the same, which means that only the help section/content should make the user go to another site:
device.domain.com/help
device.domain.com/help/version
Should make the request go to
company.custhelp.com
But for example device.domain.com/api should still work. This is what I tried. If I test the pattern inside of IIS it says that it will work. What am I missing?
<system.webServer>
<rewrite>
<rules>
<rule name="Help Redirect" stopProcessing="true">
<match url="(.*)/help(.*)" ignoreCase="true" />
<action type="Redirect" url="http://company.custhelp.com/" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
...
</system.webServer>
The match url will try to match on the path after the domain, starting from after the slash. So it should be like so:
<match url="help(.*)" ignoreCase="true" />
The match url that you wrote would match on device.domain.com/temp/help, but not device.domain.com/help.