iisurl-rewritingurl-rewrite-moduleiis-8.5

Rewrite URL to point to another site in IIS


I have two sites set up in IIS 8.5, which for the sake of simplicity let's call Test 1 and Test 2.

Test 1 can be accessed at test1.mydomain.com, and Test 2 can be accessed at test2.mydomain.com, but I want it to also be available at test1.mydomain.com/app.

This shouldn't be a redirect, but a rewrite - when the user is viewing the Test 2 site, they should see the URL test1.mydomain.com/app in their browser address bar, and all paths should use this as the base url, e.g. test1.mydomain.com/app/page1

I was hoping that this would be simple with the URL rewrite module, but it doesn't appear to be possible to rewrite the domain, only the path:

    <rule name="test2" stopProcessing="true">
        <match url="^app(/.*)?$" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
        <action type="Rewrite" url="test2.mydomain.com{R:1}" appendQueryString="false" />
    </rule>

Adding this to the web.config of Test 1 results in a 404 error, I assume because it rewrites the URL as test1.mydomain.com/test2.mydomain.com

How can I rewrite the URL to point at the Test 2 site?


Solution

  • The domain name cannot be rewritten using URL Rewrite, and URL Rewrite can only rewrite the content behind the domain name.

    You can use the reverse proxy in ARR to achieve this function:

    enter image description here

    Here are the steps to configure reverse proxy:

    1. If you have not installed ARR, you need to install it first.

    2. Double-click ARR after successful installation:

    enter image description here

    1. Set up reverse proxy:

    enter image description here

    1. Enable reverse proxy:

    enter image description here

    1. Finally, configure a URL Rewrite in your site:

    enter image description here

    For more information about "Reverse Proxy with URL Rewrite v2 and Application Request Routing", you can refer to this link.

    UPDATE:

    You can enable FRT to see what the rewritten URL is.

    Here is a demo:

    enter image description here

    enter image description here

    enter image description here

    You can try to use this rule:

    <rule name="test2" stopProcessing="true">
            <match url="^app(/.*)?$" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="Rewrite" url="http://test2.mydomain.com{R:1}" appendQueryString="false" />
        </rule>
    

    Use this action:

    <action type="Rewrite" url="http://test2.mydomain.com{R:1}" appendQueryString="false" />
    

    Don't forget to add http://.