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: 8% [?]
Entries (RSS)
great post, i like your points. i chose not to move, i think that the address with consist of my site is good for publicity so people will connect my site to my blog.
I am a newbie in PHP and actually in everything related to website development.
I always had that doubt, when I saw websites like yourname.justgotowned.com
By the way, talking about “/my-blog-post/” I have always been wondering how Wordpress or most blog systems do that redirection.
I know how to do it manually, in .htaccess , but I think that in the case of Wordpress / blog posts it is done automatically.
How is that possible?
Thanks,
a noob
Wow! This has been what I wanted to know… I have been thinking how blogspot did their subdomains… Thanks!
I believe Wordpress checks the “requested file” — for example, in this case it is /how-to-use-wildcard-subdomains/
It then checks to see if it is a real directory or a real file, if not, it redirects to index.php which then reads it as the “post slug” (the URL-ized version of the post title). It queries the database looking for a post with the same slug, then displays that post. If it can’t find it then it just displays the main page (index.php).
I think this is not unique solution, everyone is working with somekind of subdomains (i mean on blog services). But Blogspot is the best.
Great article! I’ve been looking all over for subdomain redirects. My case is slightly different than yours. My problem is that I need to carry over the name of the subdomain to the new rewrite rule. So I have several blogs on one server with Wordpress MU and i need to redirect
*.mysite.com to
*.mysite.com/wpmu
One problem is I have to deal with http://www.mysite.com AND john.mysite.com and bill.mysite.com etc. for all of the blogs. So I already have rewrite rules to redirect all requests starting with “www” to go to “mysite.com”. But how do I redirect all dynamically created subdomains to subdomain.mysite.com/wpmu ? Thanks in advance for any help.
Nick
Thanks … I was looking for something like this for the new user system I’m implementing on one of my sites.