This snippet shows an easy way to rewrite every request to a sub-domain. This can be very useful if you are, for example using a CMS and doing an upgrade but you need the URLs to remain unchanged after the switch. This article assumes that you have the rewrite module enabled on your server - which is Apache of course.
The examples shown below pertain to the following scenario, but you can modify the code accordingly to best suit your own needs. I am currently using Drupal v5x and I wish to upgrade to v6x. I do not want to over-write any files and am trying to simulate a clean install. My default installation is the root / web directory on my server. I am going to unpack the new Drupal files to a new directory: drupal6x.
On completion of the installation, I would then change my .htaccess file in the root directory to point to the new Drupal installation directory.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ /drupal6x/$1 [L]
</IfModule>
The server will choke on the rewrite if there isn't another .htaccess file in drupal6x directory. Drupal has one already, so there shouldn't be a need to change anything. However, if you are not using Drupal and do not wish any further overhead then you can use the following to prevent a never-ending loop on the rewrite....
<IfModule mod_rewrite.c>
RewriteEngine off
</IfModule>
If you are using Drupal then you may need to check file paths. Also with additional conditions it should be possible to re-use existing files too. Good luck!!
Ironically, I have had this as an unpublished article since 2008-02-26, however a post by jharnois over at Dev Shed reminded me off it, so I thought I would publish now. Hope you find it useful.
You will find other great posts over at Dev Shed, so why don't you check the forums out some time.