Rewrite Maps in IIS7 - Not redirecting - redirect

I'm trying to use a separate config file, named "rewritemaps.config", that contains the URLs I want to redirect. The file is in the root directory (same place as the web.config). The format of the Redirects.config file I have is:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="aspx/drvmain.aspx"
value="http://www.newdomain.com/folder2/page2.aspx" />
<add key="aspx/jobs_AboutUs.aspx"
value="http://www.newdomain.com/folder1/jobs.aspx" />
<add key="aspx/page.aspx"
value="http://www.newdomain.com/folder1/page2.aspx" />
</rewriteMap>
</rewriteMaps>
In my web.config file, I have:
<system.webServer>
<rewrite>
<rewriteMaps configSource="rewritemaps.config" />
<rules>
<rule name="Redirect rules">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" redirectType="Permanent"
url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewriteMaps>
</rewrite>
</system.webServer>
In the IIS Manager (which, yes, I have installed the URL Rewrite module), I've even gone and tested the rule and condition against one of my URLs in the redirects.config file and it says it works. But then when I try going to the URL in my browser, it doesn't redirect as I've specified. In the rewritemaps.config file, I've tried putting the full domain, and I've tried with a "/" before aspx. Nothing seems to work. I'm not sure what I'm missing here.

Well I got it working. Was something stupid. In the rewritemaps.config file, I needed "/"s before the key URL. I know I had tried that before, but I must have been missing something somewhere else at the time. Oh well. It works now.

It is really hard to determine why this is failing but the best thing you can do is use Failed Request Tracing so that IIS tells you what is happening. With FREB you will get detailed tracing telling you which rules were evaluated and if they matched or they didn't and why not. You can also see the "right" patterns to match and every condition that is evaluated.
See the following link:
http://www.iis.net/learn/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules

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.