•Moving a site's root using mod_rewrite

There are many tutorials and documents on the web telling you how to make the most of mod_rewrite. Unfortunately it's quite an esoteric skill and one that you will probably rarely practice. Because of that you must take care not to reinvent the wheel all the time.

You need to put this content in a file in the root folder (usually "public_html" or "httpdocs") and call that file ".htaccess" (note the dot). For some FTP applications you may have to upload the file and rename it in situ.

# .htaccess main domain to subfolder redirect # fixing different document root for website Options +FollowSymlinks RewriteEngine on RewriteCond %{HTTP_HOST} ^(www.)?example.co.uk$ RewriteCond %{REQUEST_URI} !^/first/second/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /first/second/$1 RewriteCond %{HTTP_HOST} ^(www.)?example.co.uk$ RewriteRule ^(/)?$ first/second/index.html [L]

•RewriteEngine On!

Here comes the science bit ... now concentrate!

Options +FollowSymlinks RewriteEngine on

These lines ensure that mod_rewrite is actively being used by the server, often the +Followsymlinks is set as the default and so is not required.

RewriteCond %{HTTP_HOST} ^(www.)?example.co.uk$ RewriteCond %{REQUEST_URI} !^/first/second/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d

If the host address used is "example.com" or "www.example.com" then continue processing. If the URI includes the given folder names then continue processing. If the filename typed by the user isn't a file (!-f) nor a directory (!-d) name in the current context, continue processing.

RewriteRule ^(.*)$ /first/second/$1

Ensure that whatever is typed is transparently added to teh end of the "first/second" string when searching for the files to send back to the web user. The address bar in their browser will not reflect this change in search location.

RewriteCond %{HTTP_HOST} ^(www.)?example.co.uk$ RewriteRule ^(/)?$ first/second/index.html [L]

If the condition is true that your looking at the root of the site, then send the web user the index page specified. The "[L]" ensures that we stop processing the rewriting of page addresses with this line. Other such rewrite rule adjustments include NC for case-insensitivity; C for chaining conditions together; R for redirects (updates location shown in the browser).

This is probably one of the more simple examples of use of mod_rewrite however you'll see there are several complications, not the least of which being that a familiarity with regular expressions (aka "regex", see also the regex wikipedia entry) is probably a must to understand these lines of "code".

•Updating a website

If you are using this system and want to update your website. You can work on a copy of the website, eg in "first/new-website-2008". You'd then be able to access the new website by going to http://example.com/first/new-website-2008/index.html . You'd also be able to make a near seamless change from one site to the other by simply changing all references to "second" in the script to read "new-website-2008" instead.

When you update your website it's best from an SEO and user-friendliness perspective to keep your old links working (those not being replaced by new content anyway). At least you should offer a decent "your page has moved" page. Either way mod_rewrite can step in to the fold once more ... but that's a topic for another page.