7

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

2
  • You can do it using the Rewrite module for IIS. Commented Mar 6, 2013 at 21:08
  • any example please..? Commented Mar 7, 2013 at 6:47

1 Answer 1

12
+100

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.

Sign up to request clarification or add additional context in comments.

8 Comments

@MayankPathak When you say "without actually creating subdomains on server", what do you want the user to see then? How should the server know what content to render?
@nicolas.. What i want is, there will be a page and product will vary on page by query string. And based on query string i want to make subdomains...i just want to rewrite URL as subdomain..
@MayankPathak What is still unclear to me is: Your real URL (the one that works) is http://foo.com/xyzPage.aspx?barvalue=yehaa and you want your users to see it as http://yehaa.foo.com/ in their browser? Or is it the other way? Or did I miss something?
Can you try to use the failed request tracing tool to see where does the 404 come from? It is a 5min setup and can help a lot.
I am into similiar situation. that is 404. But I am suspecting its because my server is shared by two domains. Can anybody list the bindings that i need to have on my website ? I suspect i am missing some binding as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.