问题
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.
回答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.
来源:https://stackoverflow.com/questions/17154711/how-can-i-achieve-different-url-start-in-the-same-domain