I know there have been a few threads on this before, but I have tried absolutely everything suggested (that I could find) and nothing has worked for me thus far…
With that in mind, here is what I’m trying to do:
First, I want to allow users to publish pages and give them each a subdomain of their choice (ex: user.example.com). From what I can gather, the best way to do this is to map user.example.com to example.com/user with mod_rewrite and .htaccess – is that correct?
If that is correct, can somebody give me explicit instructions on how to do this?
Also, I am doing all of my development locally, using MAMP, so if somebody could tell me how to set up my local environment to work in the same manner (I’ve read this is more difficult), I would greatly appreciate it. Honestly, I have been trying a everything to no avail, and since this is my first time doing something like this, I am completely lost.
Some of these answers have been REALLY helpful, but for the system I have in mind, manually adding a subdomain for each user is not an option. What I’m really asking is how to do this on the fly, and redirect wildcard.example.com to example.com/wildcard — the way Tumblr is set up is a perfect example of what I’d like to do.
As far as how to set up the DNS subdomain wildcard, that would be a function of your DNS hosting provider. This would be different steps depending on which hosting provider you have and would be a better question for them.
Once you’ve set that up with the DNS host, from your web app you really are just URL rewriting, which can be done with some sort of module for the web server itself, such as isapi rewrite if you’re on IIS (this would be the preferred route if possible). You could also handle rewriting at the application level as well (like using routing if on ASP.NET).
You’d rewrite the URL so
http://myname.example.comwould becomehttp://example.com/something.aspx?name=mynameor something. From there on out, you just handle it as if the myname value was in the query string as normal. Does that make sense? Hope I didn’t misunderstand what you’re after.I am not suggesting that you create a subdomain for each user, but instead create a wildcard subdomain for the domain itself, so anything.example.com (basically
*.example.com) goes to your site. I have several domains setup with MyDomain. Their instructions for setting this up is like this:I have some that are setup in just this way, having
*.example.comgo to my site. I then can read the base URL in my web app to see thatryan.example.comis what was currently accessed, or thatbill.example.comis what was used. I can then either:Does that make sense? I have several sites set up in just this exact way: create the wildcard for the domain with the DNS host and then simply read the host, or base domain from the URL to decide what to display based on the subdomain (which was actually a username)
Edit 2:
There is no way to do this without a DNS entry. The "online world" needs to know that
name1.example.com,name2.example.com,…,nameN.example.comall go to the IP address for your server. The only way to do this is with the appropriate DNS entry. You have to add the wildcard DNS entry for your domain with your DNS host. Then it’s just a matter of you reading the subdomain from the URL and taking the appropriate action in your code.