Andrej Kovalksy
Andrej Kovalksy

Reputation: 225

IIS redirect from subdomain to path

Suppose i have real subdomain like this.

test.example.com, is it possible to write a rule for iis to redirect subdomain to test.example.com/SomeStaticPage.html

the rule should work for multi level subdomains, ie. test.aaa.bbb.example.com should be redirected to test.aaa.bbb.example.com/SomeStaticPage.html

Upvotes: 0

Views: 698

Answers (1)

Carlos Aguilar Mares
Carlos Aguilar Mares

Reputation: 13581

Using URL Rewrite is very easy, in fact I'm wondering if I don't get the requirements exactly. (do you need different static pages per domain?, or is it really just to redirect to the same "SomestaticPage.html" while keeping the host name when no default page is specified?)

A simple rule like this would do what your question asks:

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="RedirectToSomeStatic" stopProcessing="true">
                    <match url="^$" />
                    <action type="Redirect" url="/SomeStaticPage.html" />
                </rule>
            </rules>
        </rewrite>
    <system.webServer>
</configuration>

Now, if you were looking for a more complete solution that allows you to specify a page based on the host name, then you could use a rewrite map where you add the host names that should be redirected and which page they should go to. For example the rule below will check if they are requesting "/" and the host name is in the rewrite map and will use that page to redirect, if it is not in the list then it will not do anything:

<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="RedirectToSomeStatic" stopProcessing="true">
                <match url="^$" />
                <action type="Redirect" url="{C:0}" />
                <conditions>
                    <add input="{HostMap:{HTTP_HOST}}" pattern=".+" />
                </conditions>
            </rule>
        </rules>
        <rewriteMaps>
            <rewriteMap name="HostMap">
                <add key="test.example.com" value="/SomeStaticForTestExample.html" />
                <add key="test.a.b.c.example.com" value="/SomePageForTestABC.html" />
            </rewriteMap>
        </rewriteMaps>
    </rewrite>
</system.webServer>

Upvotes: 1

Related Questions