ht access mod rewrite subdomain to variable

I am looking for some help with a new site I am developing. What I need to do is have members to the site being able to have their own subdomain for each account: for example

What I have been looking at is being able to use PHP to write the htaccess file and rewrite the URL to a URL with a variable with out actually creating the subdomains. I supose like creating a fake subdomain for each account holder.

Therefore I think the way to do it is with .htaccess to create something like if the user enters  they get redirected to where alll the information for the account is pulled from a mysql database using the variable 1234

What I have no clue how to do and after searching the web I cannot work out how to do the subdomain rewrite of this.

Any help to get me going would be great

Who is Participating?
gr8gonzoConnect With a Mentor ConsultantCommented:
Hmmm - you can do this (and it's actually a nice way of doing it - I use the "fake" subdomain method myself), but you should probably understand the whole setup process first.

Either way you go, you'll need to have the DNS set up for those fake subdomains. Let's say Joe is the holder of account #2. Joe's PC / network needs to be able to look up via DNS to see which server is hosting the application.

Most DNS providers make this easy to set up by creating a DNS record on for wildcards. Usually, it's as simple as creating a new subdomain called * instead of a real name like account8. Then, you make that subdomain point to the IP address of your server.

Once Joe's PC discovers the wildcard DNS and sends his browser to your server, his browser sends what host name he used ( to the web server.

(If you're using SSL, this gets a tiny bit more complicated, so if you're planning on using SSL, you may want to say so now)

The web server reads in the host name  ( and matches it up to an entry in its configuration files. Since you're using Apache, it would probably match up to a <VirtualHost> entry with the correct IP address and a ServerName or ServerAlias of *

NOW the browser's request is finally handled by a file on the server, like .htaccess. By this time, though, it would be far more efficient for PHP to just look up the subdomain instead of using mod_rewrite in an .htaccess file.

While you -can- use a mod_rewrite, two things to consider if you'll have a lot of visitor traffic:

1. Using .htaccess files slows down the web server request. If you use the Apache config variable AllowOverride to allow htaccess files, Apache will have to do some extra processing every time the browser tries to get a file from that site. So if the browser gets, it will undergo the same .htaccess processing, even if there's no resulting difference.

(The act of Apache having to check for an .htaccess file and process it if its there is slower than just delivering content right away.)

2. Mod_rewrite is pretty cool, but it's also slow.

If you don't think you'll have a lot of traffic (e.g. hundreds of visitors every second), then you probably won't notice a difference, but sometimes it's good to learn and start using more efficient ways of doing things so you don't even have to think about them for later projects.

Here's some code for whichever path you choose:

METHOD #1: PHP to extract the subdomain (better)
$pieces = split("\.",$_SERVER["HTTP_HOST"];
$subdomain = $pieces[0];

METHOD #2: Using .htaccess (untested, but it should work)
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^account([0-9]+)
RewriteRule ^(.*)$ [R=301,L]
flashjordanAuthor Commented:
This was perfect, great thanks for putting me in the right direction
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.