Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rewrite URL as subdomain in asp.net without actually creating a subdomain on server

I hope this isn't the first time i'm asking this question on SO.

I've URLs in my website and which are using query string values For example: http://foo.com/xyzPage.aspx?barvalue=yehaa

to

http://yehaa.foo.com/

Please suggest how it can be accomplished without actually creating subdomains on server ..

I've IIS 7.5 installed on server machine and using Asp.net 4.0.

Many thanks

like image 911
Mayank Pathak Avatar asked Mar 06 '13 13:03

Mayank Pathak


1 Answers

EDIT following our comments:

To access http://foo.com/xyzPage.aspx?barvalue=yehaa using http://yehaa.foo.com/, you have to use the following rule:

<rules>
    <rule name="Rewrite subdomains">
        <match url="^/?$" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^(.+)\.foo\.com$" />
        </conditions>
        <action type="Rewrite" url="http://foo.com?barvalue={C:1}" />
    </rule>
</rules>

It matches every url ending or not with a / and using something before foo.com and then rewrites it to http://foo.com?barvalue={C:1} where {C:1} is whatever value was entered before foo.com.

If you want to prevent people from accessing directly to http://foo.com?barvalue={C:1}, you can use the rule below.


You could use the Rewrite module for IIS by adding the following rule in your web.config file:

<rewrite>
    <rules>
        <rule name="Redirect to Subdomains" stopProcessing="true">
            <match url="^xyzPage.aspx$" />
            <conditions>
                <add input="{QUERY_STRING}" pattern="^barvalue=(.+)$" />
            </conditions>
            <action type="Redirect" url="http://{C:1}.{HTTP_HOST}" appendQueryString="false" />
        </rule>
    </rules>
</rewrite>

It checks if the url matches exactly xyzPage.aspx (nothing before or after).
It checks if the querystring contains the barvalue parameter (and only this one) and if its value is not empty.
If those 2 conditions are ok, it triggers the Redirect to http://barvalue.original.host.

Your question specify Rewrite, so if this is really what you want to do, change the action type="Redirect" to type="Rewrite".

Important: you may need the Application Request Routing module installed and setup with the proxy mode enabled to Rewrite to a different domain.

like image 89
cheesemacfly Avatar answered Oct 06 '22 02:10

cheesemacfly