MathBob
MathBob

Reputation: 107

IIS web.config : Rewrite URL file as subdomain

I am trying to rewrite my url with a web.config file.

Actual URL: sudomain.mydomain.com/legal.html

Wished URL: legal.mydomain.com

I have already hide the html extension.

<rule name="Hidehtml" enabled="true">
      <match ignoreCase="true" url="Legal" />
          <conditions>
             <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
             <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
             <add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
           </conditions>
       <action type="Rewrite" url="Legal.html"/>
</rule>

I saw this kind of rewrite is possible with subfolder, but I wonder if it is possible with a file. I am trying this one but it give me a DNS error.

 <rule name="testrule" stopProcessing="true">
        <match url="legal(.*)" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="mydomain.com" />
        </conditions>
        <action type="Redirect" url="http://legal.mydomain.com" />
    </rule>

I wonder if I am not missing something, maybe a redirect is needed too?

Thanks for your help !

Upvotes: 1

Views: 525

Answers (1)

Jokies Ding
Jokies Ding

Reputation: 3494

To achieve this, I just registered two CNAME for my website. FQDN:subdomain.mydomain.com FQDN:legal.mydomain.com

enter image description here

Then I apply them to my IIS site.

enter image description here

Just make sure both of them can be accessed in web browser.

Then I apply following rewrite rules

  <rules>
                <rule name="testrule" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{URL}" pattern="^/legal\.[a-zA-Z]+" />
                        <add input="{HTTP_HOST}" pattern="subdomain.candy.com" />
                    </conditions>
                    <action type="Redirect" url="http://legal.candy.com" />
                </rule>
                <rule name="rewrite rule">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="legal.candy.com" />
                        <add input="{URL}" pattern="^(/)?$" />
                    </conditions>
                    <action type="Rewrite" url="http://subdomain.candy.com/legal.html" />
                </rule>
            </rules>

When you access sudomain.mydomain.com/legal.html, the request will be redirected to legal.mydomain.com/ and legal.mydomain.com will rewrite back to /legal.html for response content.

Please keep in mind that legal.mydomain.com is always required to be registered in DNS even you just use it as a mask.

If your website are forwarding the public internet, please remember to purchase domain for legal.domain.com as well.

enter image description here

Upvotes: 2

Related Questions