Reputation: 17804
I'm trying to route http://www.domain.com/some/url(.*)
(everything behind /url
) to http://some.other.com/some/url{R:1}
(where {R:1} becomes the contents of (.*) in the matched url.
I tried the rule below and all sorts of variations but none seem to rewrite correctly:
<rule name="Reverse Proxy" stopProcessing="true">
<match url="^some\/url\/(.*)" />
<action type="Rewrite" url="http://some.other.com/some/url/{R:1}" />
</rule>
Any help is greatly appreciated!
Upvotes: 0
Views: 8300
Reputation: 1725
I had struggled on this for several days. 10-20 rewrite rule I tried and reasons of failure are:
If you trying redirecting in VisualStudio(2012/2013/2015) it could not work in actual IIS hosted site as VS generates its own cert while debugging(when you specify in project properties) as well as permission issues are taken care by VS.
The site in IIS should have valid (no copy paste of file from thawte/verisign enabled website or even self signed generated by snk.exe) certificate; please don't assume that without valid cert you can. (self signed (also known as dev cert) in IIS 8 and 10 worked for me; difference between purchased and self signed is here). The certificate should be installed as IIS can have multiple certs but each web site should use its own separate cert.
The site bindings should have both http(80) and https(443)
Now redirection syntax comes in picture; several are out there on Internet; you can get the correct regular expression easily
Another side of story also has to be considered that redirecting can be handle using Global.asax->Application_BeginRequest or ActionFilter in MVC 4/5.
Doing redirection using config or programmatically can lead to different errors(TOO_MANY_REDIRECTS
, <validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
in web.config)
Another gotcha I faced is redirecting from http->https is working fine but I am not able to comeback from https->http;
Consider your scenario(and generally should not mix) out of the available choices
HttpRedirect::
Request 1 (from client): Get file.htm
Response 1 (from server): The file is moved, please request the file newFileName.htm
Request 2 (from client): Get newFileName.htm
Response 2 (from server): Here is the content of newFileName.htm
UrlRewrite::
Request 1 (from client): Get file.htm
URL Rewriting (on server): Translate the URL file.htm to file.asp
Web application (on server): Process the request (run any code in file.asp)
Response 1 (from server): Here is the content of file.htm (note that the client does not know that this is the content of file.asp)
whether you need HttpRedirect or UrlRewrite
https://weblogs.asp.net/owscott/rewrite-vs-redirect-what-s-the-difference
Upvotes: 1
Reputation: 11762
Your regex may be the issue.
If you want to redirect everything after some/url
then use:
<rule name="Reverse Proxy" stopProcessing="true">
<match url="^some/url(.+)$" />
<action type="Rewrite" url="http://some.other.com/some/url/{R:1}" />
</rule>
If you want to redirect everything after some/url/
and keep the path, then you can use:
<rule name="Reverse Proxy" stopProcessing="true">
<match url="^some/url/(.+)$" />
<action type="Rewrite" url="http://some.other.com/{R:0}" />
</rule>
You can easily test your pattern with the IIS test pattern tool.
http://www.iis.net/learn/extensions/url-rewrite-module/testing-rewrite-rule-patterns
EDIT
What I have done to test the second rule:
test.com
domain to redirect to my server (using the host
file)Giving the following configuration in the web.config
file:
<rule name="test" stopProcessing="true">
<match url="^some/url/(.+)$" />
<action type="Rewrite" url="http://www.google.com/{R:0}" />
</rule>
http://test.com/some/url/google
with a browser:
It shows that the URL is rewritten using Google as destination and taking as parameter the path first requested.
Upvotes: 2