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

Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67

1 Answers1

12

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.

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • @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? – cheesemacfly Mar 11 '13 at 14:53
  • @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.. – Mayank Pathak Mar 12 '13 at 05:52
  • @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? – cheesemacfly Mar 12 '13 at 14:14
  • I just installed `ARR` and configured it..but it shows `HTTP Error 404.4 - Not Found` error after setting up `ARR`. But before installing it was working as redirect to `subdomain.` Please help – Mayank Pathak Mar 28 '13 at 07:34
  • @MayankPathak have you enabled the proxy mode? – cheesemacfly Mar 28 '13 at 13:50
  • 1
    Can you try to use the [failed request tracing tool](http://www.iis.net/learn/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules) to see where does the 404 come from? It is a 5min setup and can help a lot. – cheesemacfly Mar 28 '13 at 14:05
  • Thanks for suggesting tracing tool..it really helped me a lot..at least now i'm able to see where i'm lacking..Sorry Mate, i'm marking again your [ost answer :) will post another question with that specific error....rule works fine, rewrites the url but something again rewrites URL to original URL...that error will post as new question.. – Mayank Pathak Mar 29 '13 at 10:00
  • 1
    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. – Majid Apr 12 '17 at 10:32