Imagine you have a website and a subdirectory (say, http://example.com/gallery/) and you now want to make use of a subdomain instead (say, http://gallery.example.com)
Doesn't sound too hard, does it?
But this kind of change can cause a fair few headaches - not least of which because you might forgot one (or more) links somewhere which need to be modified (and so your users end up with 404 errors). Search engines also have problems - they don't always update the links in search results (although you can at least partly solve this by submitting a new sitemap), and any page ranking you gained for the old link will be lost.
Here's a simple solution to remove those headaches:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond %{REQUEST_URI} ^/gallery
RewriteRule gallery/(.*) http://gallery.example.com/$1 [L,QSA,R=301]The above code first checks that the request to the main domain (example.com). Then it checks if you are are requesting the gallery directory on that domain. Then it rewrites with whatever comes after the 'gallery' directory in the URL, the querystring, and sends a 301 header to the client.
This last bit is the important bit - sending a 301 tells the client that this new URL should be used instead of the old one from now one. Browsers like Firefox and Internet Explorer will keep a note of this, and so will automatically request the subdomain URL instead of the original URL.
But it is much more useful for search engines - when they get a 301 header, they will move whatever page score that the old URL had and update it. They will also update their records so the results point to the right URL again (although, this doesn't matter so much as your server will deal with requests to the old one).