This seems to be like the simplest of thing which I am unable to resolve.
I want to rewrite URL if it contains the string extFlow, to a different host.
http://site1/extFlow/Testsvc/GetData/?val=1
to
http://site2/extFlow/Testsvc/GetData/?val=1
I have tried all rules eg:
<rules>
<rule name="Reverse Proxy to external" stopProcessing="true">
<match url="extFlow/(.*)" />
<action type="Rewrite" url="http://site2/{R:1}" />
</rule>
</rules>
If I just match with (.*) then the rewrite happens.
I am not able to match even with * match url="extFlow"
What am I missing?
Based on your statement "Yes for testing purpose I have both sites on same machine, but on a different port", I think that's what is breaking your rule. Let me disintegrate and explain it.
Observe how you have "extFlow" in both your URL's.
Let's assume a request came in for
"http://site1/extFlow/Testsvc/GetData/?val=1". The URL rewrite rule
is hit and its rewrites it to
"http://site2/extFlow/Testsvc/GetData/?val=1".
Since this new URL also hits the same machine and even it has
"extFlow" in it, the rule will be triggered once again and again and
again.
So indirectly you are triggering an indefinite loop here.
Let me know if you have any questions.
Related
Currently working with an external file for URL rewrites called rewritemaps.config (following these instructions), which works great. However, we have occasion to override an entry in that file with manual entries in web.config instead and they don't appear to work.
Specifically this issue has cropped up around the use of content experiments (A/B testing) with Google, which makes heavy use of querystrings in my URLs. The querystring takes the form of:
websiteurl.com/webpage1?utm_expid=########-#&utm_referrer=http%3A%2F%2Fwebsiteurl.com%2F
Now when attempting to validate my content experiment, I get the error
Web server rejects utm_expid. Your server doesn't support added query
arguments in URLs.
...which is true for that particular URL because in the rewritemaps.config file we have this line:
<add key="/webpage1" value="/somefolder/file.aspx?id=18" />
The rewrite rule works fine but no amount of manual entries in web.config seem to work. If I comment out the line in rewritemaps.config and attempt to reach /webpage1, it errors with 404..which is expected. But I try to use manual entries in my web.config like:
<rule name="test rewrite" stopProcessing="true">
<match url="^(https?:\/\/)?websiteurl.com\/webpage1(.*)*" />
<action type="Rewrite" url="{R:1}/somefolder/file.aspx?id=18" appendQueryString="false" />
</rule>
I'm placing the rewrite rule before
<rewriteMaps configSource="rewritemaps.config" />
I've added query_string conditions, appendQueryString to true and to false, nothing is working even if I use wildcard patternSyntax, so it's leading me to believe that I can't manually add rewrite rules into the web.config if there is an external rewritemaps.config file.
Is this true? If so, how can I manually override a rewrite rule because I have to add the ability to allow a querystring in a rewrite?
Well after much consternation and research, I stumbled upon this question and the highest voted answer, and it's working for me now with the rewrite rule still in rewritemaps.config.
Changing {REQUEST_URI} to {PATH_INFO}
In IIS 7.5 (Win2k8 R2) I'm trying to create a new rule for these requests:
http://www.domain.com/userprofile
https://www.domain.com/userprofile
http://domain.com/userprofile
https://domain.com/userprofile
rewriting to:
http://www.domain.com/users/?username=userprofile
(or whatever the protocol/domain is)
The regex that I wrote is:
^(http|https)://(www\.domain.com|domain\.com)/([a-zA-Z0-9-]{6,35})
and the rewrite is:
{R:1}://{R:2}/users/?username={R:3}
But this is not working. Is it because I don't need to the protocol? I also added conditions that the request is not a file or directory.
Also, do I need to restart IIS each time I change the rule?
You don't have to look at the protocol or domain name when you want to rewrite these request. It's not important in your case as you only want to rewrite the path.
The following rule should work:
<rule name="Rewrite user profiles">
<match url="([a-zA-Z0-9-]{6,35})" />
<action type="Rewrite" url="/users/?username={R:1}" />
</rule>
You don't have to restart IIS when you change the rule. IIS will automatically restart the application pool when web.config is modified and hence reload the rules.
#Marco was almost right, but here's how what ended up working: (I used the URL Rewrite form in IIS Manager)
Regex:
^([a-zA-Z0-9-]{6,35})(/?)$
Conditions:
Not a file
Not a directory
This forces the match to start directly after the domain and must be either the first directory, or with no trailing "/". According to the regex, the match must be between 6 and 35 characters, alpha numeric with "-"
Trying to use AAR as a reverse proxy in front of several back end IIS servers.
One public ip address assigned to the server running IIS/AAR
Then outbound URL rewrite rules are setup to redirect to one of
several back end servers depending on hostname.
Works somewhat, but always returns the back end servers default site (not the one mapped to a hostname) so it looks like the host name (HTTP_HOST) is not getting passed from the proxy server to the back end server.
(I've verified bypassing the reverse proxy by editing hosts and the back end server returns the correct site bound to the host header)
This is an example of the rule (192.168.0.99 is the internal server, site.myco.com is the hostname)
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://192.168.1.99/{R:1}" />
</rule>
</rules>
</rewrite>
Have tried putting sever variables so
<!-- Guessing server.myco.com is hard coded -->
<serverVariables>
<set name="HTTP_HOST" value="server.myco.com" />
</serverVariables>
<!-- Guessing picked up dynamically from incoming request host header -->
<serverVariables>
<set name="HTTP_HOST" value="{HTTP_HOST}" />
</serverVariables>
But alas always returns the default binding - any ideas?
This post has the answer - Modifying headers with IIS7 Application Request Routing
Need to enable preserveHostHeader - can't see how you do that in the UI but this works
Run this from command line to update Machine/webroot/apphost config
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/proxy -preserveHostHeader:true /commit:apphost
You can do this with GUI. While on the root server click configuration editor, go to System.webServer -> proxy and set preserveProxyHeader to true.
My guess would be that your server doesn't allow you to change the server variable HTTP_HOST when you rewrite the URL.
At the level of the website where the URL rewrite is applied:
Then click the Add... link on the right tab and add your HTTP_HOST variable:
I am having an issue with the Url Rewrite module in IIS7. I have read several posts about this type of issue and I can't seem to figure it out.
Here is the rule:
<rule name="Imported Rule 11" stopProcessing="true">
<match url="/s\/([\w\%20\s]+)\/default\.aspx" />
<action type="Rewrite" url="\/landingpage.aspx\?mc={R:1}"
logRewrittenUrl="true" />
</rule>
I imported it from a previous ISAPI ini filter that looks like the following:
RewriteRule ^\/s\/([\w\-\_\%20\s]+)\/default\.aspx \/landingpage.aspx\?mc=$1 [L,I]
I had to tweak the regular expression a bit to get it to work with the built in tester but it is matches against urls like
/s/RETQ211CA/default.aspx
And according to the log the request is coming through as:
2012-10-18 15:41:54 ::1 GET /s/RETQ211CA/default.aspx - 80 - ::1 Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+6.1;+WOW64;+Trident/4.0;+SLCC2;+.NET+CLR+2.0.50727;+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30729;+.NET4.0C;+.NET4.0E) 404 0 0 1468
This happens locally or through the internet.
Can someone tell me what exactly I'm doing wrong here?
Thanks!
The following rule will do the trick:
s\/([\w\%20\s]+)\/default\.aspx
You could start this rule with ^ and terminate with $ if you'd like.
The confusion is that url rewrite assumes you're in the root.
I wish to canonicalize a domain name, from bar.example.com to www.example.com (well anything that's not www.example.com). Site runs IIS7.
The problem is that certain URLs were of the form http://bar.example.com/asp/oldpage.asp?query=awesome, and have specific URL rewrite rules already in place that redirect to http://www.example.com/newpage/awesome
I want to write a rule that catches the other rules.
HERE'S THE CATCH: I have a lot of rules, and want to put this rule in the root of the site, but have additional rewrite/redirect rules in sub-folders, so I want to defer the 301 from happening until all the rules have been run.
Is this possible? Rewrites have an option to defer (stopProcessing="false") but this doesn't seem to be an option for Redirects.
Am I SOL here?
Unfortunately, I can confirm that the deferred processing (stopProcessing="false") works with rewrite actions only and is ignored by the redirect ones.
Should the number of matches having been small - but it is not, according to your question - I would have suggested you to combine them using a regex alternation. For example:
First match: ^first/a$
Second match: ^second/b$
Combined match: ^(first/a|second/b)$
Leading to something like:
<rule name="MyCombinedRule">
<match url="^(first/a|second/b)$" />
<action type="Redirect" url="http://www.example.com/third/c" />
</rule>