When configuring a web server for different applications, the use of .htaccess files can help with specific functionality which could otherwise be quite a pain. While this could potentially make your server inaccessible when used incorrectly, simply removing the file will restore normal functionality.
Here we’ll cover these basics, mostly rewrite techniques:
- forcing the ‘www’ to show in the URL
- redirecting a website through a sub-directory
- URL rewriting for “clean URLs”
Forcing the ‘www’ to show in the URL
Why would we want to do this? Many sites that use Javascript might have run into this issue: You’re trying to make an AJAX request from your application while not at the “www version”, but the request is going to the “www version”. All of a sudden you have a Javascript error detailing that the cross-site interaction wasn’t allowed.
This is not always the case for doing this, but it is one practical reason. Sometimes you can’t avoid having this sort of cross-site interaction due to prior limitations, so you resolve it by forcing the ‘www’.
Code Example:Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This creates a permanent redirect which will let search engines know that the page should be using the ‘www’ version.
Redirecting a website through a sub-directory
Sometimes having multiple applications on a web server can get a bit hectic. You might even have multiple domains pointing to one application with different configurations. In this case separating the applications into their own directories seems like a good idea, but how to we get the URL to ignore the sub-directory?Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com
RewriteCond %{REQUEST_URI} !^(/sub-directory) RewriteRule ^(.*)$ sub-directory/$1 [L]
This will rewrite every request going to example.com to the sub-directory, so while the user sees the normal URL, the server is actually getting the file from the sub-directory, neato huh?
URL rewriting for “clean URLs”
Everyone wants the “clean URL” look now-a-days, why trouble yourself with reading query strings, when the URL could make actual sense? This technique is also known as SEO-friendly URLs.
But what if your application uses query strings? Do you have to miss out or rewrite your application? No, you don’t.
You can use .htaccess with a series of rules to accomplish the same thing behind the scenes, although it become increasingly difficult if your application doesn’t already follow a query pattern.Options +FollowSymlinks
RewriteEngine on
RewriteRule ^example/([^/]+)/([^/]+) /example.php?page=$1&action=$2 [NC]
This will cause your site to rewrite something like “website.com/example/news/save” to “website.com/example.php?page=news&action=save” in the background. More complex rewrites could be made to have this work with specific applications.
That covers some basic .htaccess tips and tricks, hope you’ve enjoyed.
References:
http://corz.org/serv/tricks/htaccess.php
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/




Related posts: