How-to: Use Wildcard Subdomains
Posted by Jon Lee in How-to, Web Development, tags: Apache, mod_rewrite, subdomain, web developmentMost websites and blogs have URIs that look like http://www.jonlee.ca/my-blog-post/. Of course, my-blog-post/ is not an actual folder on the site, but rather an Apache mod_rewrite that redirects to a post by the same name.
A neat thing you can also do with mod_rewrite (and some DNS editing) is to use wildcard or catch-all subdomains like this:
http://my-blog-post.jonlee.ca/
Alternatively, you could create individual subdomains manually through your web control panel but that wouldn’t be a very elegant nor efficient solution.
Setting up a Wildcard or Catch-all Subdomain
The set-up is pretty easy, just a simple modification of the DNS to include a record with the name:
*.jonlee.ca
Then add the same thing to your ServerAlias line in the Apache configuration. (You probably won’t have permission to perform the two previous steps if you’re on shared hosting, so ask your web host to do it for you). Finally, create a mod_rewrite that will read the subdomain as a parameter on another page, e.g.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.jonlee.ca
RewriteRule ^(.*) http://www.jonlee.ca/?p=$1 [L,R=301]
Good or Bad?
When using wildcard subdomains, each subdomain might be treated as a completely separate site by some services. This could be a disadvantage since you might not be pooling all your incoming links and stats to a single domain. Not to mention, I don’t find it as aesthetically pleasing as there isn’t a consistent “base” URI throughout the site.
On the other hand, having each subdomain count as a separate site could be an advantage as well, I’ll leave that up to your imagination. What do you think?
Popularity: 4% [?]
Entries (RSS)