bugmagnet
bugmagnet

Reputation: 7769

IIS UrlRewrite: How to rewrite from just a domain to domain and path

I have successfully written a web.config that lets me reverse-proxy a site and have it appear in a either a server's IFRAME or even on my own localhost. A reduced/redacted version of it follows.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="ReverseProxyInboundRule1" stopProcessing="false">
                <match url="(.*)" />
                <action type="Rewrite" url="https://www.example.com/{R:1}" logRewrittenUrl="true" />
                <serverVariables>
                    <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
                    <set name="HTTP_ACCEPT_ENCODING" value="" />
                </serverVariables>
            </rule>
            <rule name="Capture Origin Header">
                <match url=".*" />
                <conditions>
                    <add input="{HTTP_ORIGIN}" pattern=".+" />
                </conditions>
                <serverVariables>
                    <set name="CAPTURED_ORIGIN" value="{C:0}" />
                </serverVariables>
                <action type="None" />
            </rule>
        </rules>
        <outboundRules>
            <rule name="Rule1" patternSyntax="Wildcard" stopProcessing="false">
                <match serverVariable="RESPONSE_X-Frame-Options" pattern="*" />
                <action type="Rewrite" value="" />
            </rule>
            <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsTextHtml" stopProcessing="false">
                <match filterByTags="None" pattern="^http(s)?://www.example.com/(.*)" />
                <action type="Rewrite" value="http{R:1}://thing.our-server.com/{R:2}" />
            </rule>
            <rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">
                <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
                <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
            </rule>
            <rule name="ContentDam" preCondition="ResponseIsTextHtml">
                <match filterByTags="None" pattern="^/(content/dam/.*)" />
                <action type="Rewrite" value="https://www.example.com/{R:1}" />
            </rule>
            <rule name="Set-Access-Control-Allow-Origin for known origins">
                <match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern=".+" negate="true" />
                <action type="Rewrite" value="{CAPTURED_ORIGIN}" />
            </rule>
            <rule name="source srcset" preCondition="ResponseIsTextHtml">
                <match filterByTags="CustomTags" customTags="sourceSrcset" pattern=",?\/(content\/dam\/\S+\s\d+w)" />
                <action type="Rewrite" value="https://www.example.com/{R:1}" />
            </rule>
            <rule name="Change GTM" preCondition="ResponseIsTextAnything" patternSyntax="ExactMatch">
                <match pattern="GTM-5GFGV2" />
                <action type="Rewrite" value="GTM-5XVB5D" />
            </rule>
            <preConditions>
                <preCondition name="ResponseIsTextHtml">
                    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                </preCondition>
                <preCondition name="ResponseIsTextAnything">
                    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)" />
                </preCondition>
                <preCondition name="NeedsRestoringAcceptEncoding">
                    <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".*" />
                </preCondition>
            </preConditions>
            <customTags>
                <tags name="sourceSrcset">
                    <tag name="source" attribute="srcset" />
                </tags>
            </customTags>
        </outboundRules>
    </rewrite>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders> 
    </httpProtocol>
</system.webServer>

Currently, I point my IFRAME at https://thing.our-server.com/ and the reverse proxy takes the path and query string and hands them off to a request to https://www.example.com/ and rewrites the results (more or less) to this.our-server.com.

What I would prefer is that I can point my IFRAME at https://thing.our-server.com/example/ (plus path and query) and have everything work the same way.

However, when I make the following changes, it doesn't work:

<rule name="ReverseProxyInboundRule1" stopProcessing="false">
      <match url="example/(.*)" />
      <action type="Rewrite" url="https://www.example.com/{R:1}" logRewrittenUrl="true" />
      <serverVariables>
          <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
          <set name="HTTP_ACCEPT_ENCODING" value="" />
      </serverVariables>
</rule>

and

                <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsTextHtml">
                    <match filterByTags="None" pattern="^http(s)?://www.example.com/(.*)" />
                    <action type="Rewrite" value="http{R:1}://thing.out-server.com/examle/{R:2}" />
                </rule>

I expect it's a misspecification, but I can't see it at the moment.

Upvotes: 2

Views: 334

Answers (1)

Jalpa Panchal
Jalpa Panchal

Reputation: 12749

if you have anything after the example/ then you could use the example/(.*) regular expression. otherwise, just use example in a match rule.

and the another point there is typing mistake in below rule:

 <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsTextHtml">
                    <match filterByTags="None" pattern="^http(s)?://www.example.com/(.*)" />
                    <action type="Rewrite" value="http{R:1}://thing.out-server.com/examle/{R:2}" />
                </rule>

http{R:1}://thing.out-server.com/examle/{R:2} use the example.

Upvotes: 1

Related Questions