0

Let's suppose I develop a website, let's suppose it has a domain of foo.bar.

Let's suppose further that I have a user called lorem and a user called ipsum. I would like to create an URL of lorem.foo.bar for lorem and a URL of ipsum.foo.bar for ipsum. What are the possible technologies with which I can achieve that? Can you guys give any example in any technology where this is achieved?

Thanks so much.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • This is an overly broad question and how you achieve will depend upon everything from what platform you are using (IIS, Apache), who you are hosting with, what control panel software you are using, and so on. Can you be a little bit more specific? – dparsons Jun 17 '13 at 19:31
  • No, I cannot be more specific, as I am about to choose the technology. If somebody can give me the possible technologies I can use, then I can choose one from them. So, my question is technology-agnostic and an ideal answer would contain the options and hopefully an example. I absolutely do not know how can I achieve this and would be very happy if an answer would enlighten me. – Lajos Arpad Jun 17 '13 at 22:05

1 Answers1

1

Depending on your needs I find this approach to be the most straightforward.

In my experience the best way to do what you are trying to do is through Wildcard domains which is configured at your DNS server. In your example *.foo.bar would be the wildcard for your foo.bar domain. Typically these are setup on A records but can be setup on MX records and the like. The only real requirement here is that your DNS server supports wildcard domains.

As far as the Webserver goes, you will need to employ some sort of URL Rewriting technique so that lorem.foo.bar displays the lorem page and so on. There are several techniques to ReWrite URLs. On Apache you can configure VirtualHosts or use mod_rewrite. On IIS there are, again, several methods using either ISAPI filters or you can use URL Routes in ASP.NET MVC to do the rewriting.

Here are some relevant examples:

Creating Wildcard Sub Domain Using Apache VirtualHost

URL rewriting in .NET 4?

http://support.microsoft.com/kb/840687

http://www.isapirewrite.com/docs/

-Edit per comment

In your example of *.foo.bar you will only ever setup a single wildcard domain on your DNS server. Once that is done you shouldn't ever have to do it again unless you add different wild cards.

As far as Apache goes you should be able to do something like this (I haven't used Apache in a long time so this might be slightly inaccurate)

<VirtualHost 0.0.0.0>
   DocumentRoot /www/[directory sub domains are served out of]
   ServerName www.foo.bar
   ServerAlias *.foo.bar
</VirtualHost>

Again this should be a one time configuration or as you add more wildcards. From here your PHP (or whatever language you are using) would need to handle the actual processing of what content it should display based on the given subdomain.

--edit 2 for second comment

So a wildcard domain allows for pretty much any value. In the above case *.foo.bar would allow for lorem.foo.bar, ipsum.foo.bar, someguy.foo.bar, and so on without requiring you to add additional entries to your DNS server or add additional VirtualHost entries. So using the VirtualHost configuration above lets say the DocumentRoot was /www/subdomains. When navigating to lorem.foo.bar or ipsum.foo.bar both requests get served out of the /www/subdomains folder so the code in there would need to grab the name of the subdomain (lorem or ipsum in this case) and then act on those values by querying the database or whatever other business processes you have in place. Attempting to create a single directory , DNS entry, and/or VirtualHost entry for this process is a mistake because it will become an absolute management nightmare. In the example above you only ever have to worry about 1 DNS entry, 1 VirtualHost entry, and one directory worth images, web pages, PHP scripts, etc.

Community
  • 1
  • 1
dparsons
  • 2,812
  • 5
  • 28
  • 44
  • +1 Thank you, that's a very nice answer. Let's suppose (just for fun) that I am using Apache (I don't know yet). If I do that how can I create a wildcard domain when a user registers? Should I write some content into .htaccess? Is there a better solution for that? – Lajos Arpad Jun 18 '13 at 02:08
  • Thank you, but maybe I was unclear. I would like to automatically create wildcards. When foo registers, I would like the system to create a wildcard for him automatically. When bar registers, I would like to create a wildcard for him automatically and so on. I understand that in the virtualhost tag I can edit the wildcards, but is there an automatic way to add/remove wildcards? Thanks. – Lajos Arpad Jun 19 '13 at 01:02