BLOG

Dynamic Sub Domains

11th of January 2007
Tags: ,

Hi and Welcome

Apparently in 2007 blogs are going to peak, meaning after that they will decline. As a web designer I have decided I don’t want to miss out and have decided to build up a blogging empire! I have 4 main objectives in writing these blogs as listed below:

1. As another creative outlet and a means to share ideas with other people

2. To generate traffic to help boost all of the other sites I build

3. To possibly generate some revenue through google adsense and other advertising

As this is the first post, probably no one will be reading this except myself and possibly few search engines, but over the course of the next couple of months I am going to employ various techniques and strategies to boost the amount of traffic. I will be listing these techniques and ideas here in the hope that someone else will find them useful.

In this post I want to talk about something I only found out about the other day, dynamic sub domains. As you probably know, this site itself is hosted on a sub domain, http://blog.voodoochilli.com. The actual domain www.voodoochilli.com is my company website and the blog part is the sub domain. I know from experience that sub domains can rate very highly search engine wise, and they are a very cheap alternative to buying a fully qualified domain as there are no extra registration costs (although your hosting company may charge you a set up for a sub domain)

Dynamic sub domains means that you can generate these on the fly using a .htaccess file. First though you have to make sure you have wildcard dns turned on for your domain. You can test this by typing anything.yourdomainname.com and if you don’t get a 404 page the chances are this is already set up. Once you have this in place you need to look into .htaccess.

Heres an example of a .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.yourwebsite.com
RewriteCond %{HTTP_HOST} ([^.]+)\.yourwebsite.com
RewriteRule ^(.*)$ /path_to_your_site/httpdocs/work_out.php?url=%1

Remember: This file must be called .htaccess exactly, with no file extension; when you upload it to your server it will probably be invisible so keep a backup!

The .htaccess file will then send the variable $url with the sub domain, so for example if you type in:

http://test.yourwebsite.com

This will translate to (but not be visible as)

http://www.yourwebsite.com/work_out.php?url=test

On one of my sites the work out page checks the MYSQL database for the username and simply brings up the profile for that person. As far as a visitor is concerned we are actually at http://test.yourwebsite.com. There are other ways of doing this, but after looking it up this seems the easiest.


18 responses to “Dynamic Sub Domains”

  1. Stewart Lee says:

    Good tip, a way of using mod_rewrite i didn’t know!

  2. Janos says:

    Very nice!

    Could you give me an example of how to mod_rewrite this one?

    http://test.somedomain.com/category=1&page=1

    I have no clue how to catch the parameters on the end (gallery, page)

    Thanks!

  3. […] subdomains using htaccess I found this blog  about how to create dynamic subdomain using htaccess. I will try this at home later. Actually you […]

  4. Mike says:

    going to revamp my site and am interested in how your workout.php sorted from the db and displayed content and kept the address to display test.yourdomain.com

  5. kawabray says:

    hi
    i need to do thie plz
    i have this url
    http://mysite.com/index.php?go=news
    i need this url in this photo
    news.mysite.com
    by htaccess plz tell me answer

  6. Voodoochilli says:

    Janos

    you would use php to capture the $_REQUEST variable and do with it whatever you want.For example

    http://test.somedomain.com/category=1&page=1

    Supposing the page is index.php on it just use:

    $category;
    and
    $page;

  7. Paul says:

    Hi,

    Firstly – thanks for publishing your work! I really want to use this for my site but don´t understand a few things.. perhaps you could clarify?

    1) with the line of code: RewriteRule ^(.*)$ /path_to_your_site/httpdocs/work_out.php?url=%1 … I don´t understand the “path_to_your_site” part, path from where?? is this just my domain? Or are we digging back, like var/vhosts/mydomain.com?

    2) Assuming I´ve got a dedicated server, with multiple virtual domains, where is the best place to put the .htaccess file? In the root directory of domain in which I want to use it? In the http_docs folder?

    3) With the “workout.php” file… I guess that just processes the variable and re-directs as required, so in your example, with “url=test”, you would assume there is a normal sub-directory named “test” and the php script just redirects the user to that sub-directory? Correct? Or no?

    Hope you can help…

    Thanks!

  8. Matthew says:

    Hi,
    I tried this trick with my website, trying to redirect each wordpress category to a subdomain. It doesn’t work….

  9. Paul says:

    @Matthew: – it works fine for me

  10. Voodoochilli says:

    Works for me too!

  11. Not working for me !!!

  12. Stijn says:

    Hi,

    The subdomains are working perfectly, but I still have got a problem. I would like to make URLs like:
    http://username.domain.com/page/subpage/category/

    I can make SE-friendly URLs and after reading this article I’m also able to create virtual subdomains, but I can’t manage to do both the same time. So, my question is, how can I redirect the following URL:
    http://username.domain.com/page/subpage/category/

    to:
    http://www.domain.com/index.php?user=username&get1=page&get2=subpage&get3=category

    Can anybody please help me?

  13. Voodoochilli says:

    @Stijn
    Do you still have this problem?

  14. it’s will not work with out dns job
    thanks

  15. hello , i have got the solutions to make it work with out any mod on http.conf or name server
    just go to you cpanel and add new sub domain
    name *.yoursite.com
    now when you go to any address
    like : yahoo.yoursite.com it will work’s
    thanks

  16. Rahul says:

    I am not able create pages under dynamic subdomain…
    help me!

  17. Arun KR says:

    Hi

    Sub domain not working for me

    this is the code i have used.

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.arunkrishnanivas.com
    RewriteCond %{HTTP_HOST} ([^.]+)\.arunkrishnanivas.com
    RewriteRule ^(.*)$ /arunkrishnanivas.com/login.php?user=%1

    pls help me

Leave a Reply

Your email address will not be published. Required fields are marked *

  • About Us

    We are Voodoochilli Design Ltd - the place to go for Hereford web design

    We have been designing and building websites for over 10 years and have worked with a wide range of businesses from start-ups to multi-billion pound internationals and everything in between.

    If you are interested in finding out more, check out our full website.

  • Recent Posts