ht access mod rewrite subdomain to variable

Posted on 2009-04-18
Last Modified: 2012-05-06
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

Question by:flashjordan
    LVL 34

    Accepted Solution

    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.

    STEP 1:
    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.

    STEP 2:
    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]
    LVL 1

    Author Closing Comment

    This was perfect, great thanks for putting me in the right direction

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now