I'm working on an ASP.NET MVC Multi Tenancy app.
Right now I managed to create dynamic subfolders, for instance some one registers with username "bob" and gets the following website:
domain.com/bob
My next goal is to provide subdomains: bob.domain.com instead of subfolders.
I found out that it's very complex to create dynamic subdomains with ASP.NET and DNS WMI. Is there a way to tell the server that it has to redirect/rewrite from bob.domain.com to domain.com/bob ?
Best Answer
You can create a wildcard entry for *.domain.com so that you don't need to create a DNS entry on every new account.
You can add a wildcard IIS binding too so that everything for a particular IP will bind to that site. That you don't need to add tons of host headers in real-time.
URL Rewrite is one option for doing what you requested. You can watch for {HTTP_HOST} with the pattern of ^(^.)+.domain.com$. That will get everything.domain.com with a back reference of C:1.
Or, another option is to just check for Request.ServerVariables["HTTP_HOST"] from MVC directly. You have full access to the domain name from code, so you have a lot of flexibility there.