Stuggi
Stuggi

Reputation: 215

Rewriting subdomains to subfolders and parameter in IIS

I'm trying to wrap my head around this problem;

I have a default site with an application that resides in a subfolder, e.g. /app/app.html

It also accepts a language parameter, so for example http://192.168.0.1/app/app.html?language=fi would work.

Now I have two subdomains that I need to not only get rewritten to the correct folder, but also include the language parameter. For example:

fi.domain.com -> http://1.1.1.1/app/app.html?language=fi

swe.domain.com -> http://1.1.1.1/app/app.html?language=swe

I've made A records for both subdomains to point to 1.1.1.1

Currently there are no special bindings (only port 80, no hostnames and all IPs) and no special default pages.

EDIT: I've tried using the URL rewriter module, but I haven't been able to get it work as intended.

EDIT 2: My first example of how I need it to work was a bit flawed, here's a better version;

finnishword.domain.com -> http://1.1.1.1/app/app.html?language=fi

otherwordinswedish.domain.com -> http://1.1.1.1/app/app.html?language=swe

Upvotes: 0

Views: 265

Answers (2)

Stuggi
Stuggi

Reputation: 215

<rules>
            <clear />
            <rule name="swedishMainRule" enabled="true" stopProcessing="true">
                <match url="^$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_HOST}" pattern="^(www\.)?swedishword\.domain\.fi$" />
                    <add input="{QUERY_STRING}" pattern="language=" negate="true" />
                </conditions>
                <action type="Redirect" url="?language=sve" appendQueryString="false" logRewrittenUrl="false" />
            </rule>
            <rule name="swedishRule" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_HOST}" pattern="^(www\.)?swedishword\.domain\.fi$" />
                </conditions>
                <action type="Rewrite" url="/app/{R:1}" />
            </rule>
            <rule name="finnishRule" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_HOST}" pattern="^(www\.)?anotherfinnishword\.domain\.fi$" />
                </conditions>
                <action type="Rewrite" url="/app/{R:1}" />
            </rule>
        </rules>

This is how I ended up doing it, basically the first rule deal with the language parameter, and the other rules just rewrite the URL. The Finnish rule doesn't need an extra language rule since the default language of the app is Finnish.

Upvotes: 0

Mathieu
Mathieu

Reputation: 526

I'm not sure to well understand your question. It seems that you need URL rewriting rules.

According to this link

<rule name="CName to URL - Rewrite" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.domain\.com$" />
    </conditions>
    <action type="Rewrite" url="/app/app.html?language={C:1}" />
</rule>

I think it's what you need ;)

Edit 24/08/2016 If you can't have a dynmique pattern with RegEx you have to do as many rules as you have subdomains:

<rule name="finnishRule" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(?!www)(finnishword)\.domain\.com$" />
    </conditions>
    <action type="Rewrite" url="/app/app.html?language=fi" />
</rule>
<rule name="finnishRule" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(?!www)(otherwordinswedish)\.domain\.com$" />
    </conditions>
    <action type="Rewrite" url="/app/app.html?language=swe" />
</rule>

or you can mixup all together if you want to redirect all subdomains that contains the word "swedish" on url with ?

<rule name="finnishRule" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(?!www)(.*swedish.*)\.domain\.com$" />
    </conditions>
    <action type="Rewrite" url="/app/app.html?language=swe" />
</rule>

So after all the RegEx pattern is up to you ;)

Edit 25/08/2016

Maybe you have to add condition to skip static files

<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
...
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>

Upvotes: 1

Related Questions