Ropstah
Ropstah

Reputation: 17804

IIS Rewrite Rule not behaving as expected

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

Answers (2)

Saurabh
Saurabh

Reputation: 1725

I had struggled on this for several days. 10-20 rewrite rule I tried and reasons of failure are:

  1. 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.

  2. 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.

  3. The site bindings should have both http(80) and https(443)

  4. Now redirection syntax comes in picture; several are out there on Internet; you can get the correct regular expression easily

  5. 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)

  6. Another gotcha I faced is redirecting from http->https is working fine but I am not able to comeback from https->http;

  7. 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

cheesemacfly
cheesemacfly

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:

  • Setup the test.com domain to redirect to my server (using the host file)
  • Setup the rule with IIS as follow: rules

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>
  • Reach http://test.com/some/url/google with a browser: Chrome

It shows that the URL is rewritten using Google as destination and taking as parameter the path first requested.

Upvotes: 2

Related Questions