Using Apache's mod_rewrite
mod_rewrite is a tool to let your webserver return a different thing to what was requested. It can be used to simplify urls and redirect people from old to new content, or even to make a dynamic web page appear static. In fact, this website is written using PHP templates but because the content is almost entirely static, I use mod_rewrite to make it look like a collection of static HTML files. This tutorial will be mainly focusing on how to do just that.
Why would I want my dynamic website to appear static?
- Search engines dont like dynamic pages
- Users are more familiar with .html URLs
- PHP GET requests are ugly, unmemorable and unguessable
Having said this, I am certainly not recommending that all dynamic websites should use url rewriting because that would be a nightmare; but for sites such as this one where the content is essentially static, it is a way to make a dynamically templated site more usable
Setting up mod_rewrite
If you have access to your Apache configuration, you will need the line:
LoadModule rewrite_module modules/mod_rewrite.so
uncommented in the module loading area of httpd.conf. If you are intending to change things via a .htaccess file (as this tutorial explains), you will also need
AllowOverride All
in the relevant Directory section.
If you do not have control over your Apache configuration, then you will need to contact your system administrator or just try it and see if it works
The .htaccess file
You will need to create an .htaccess file in the relevant directory, or add to the one that is currently there. You will need the RewriteEngine directive to enable URL rewriting for this folder (and all subfolders) and the RewriteRule directive to add a rewriting rule. The rewriting rules use regular expressions to control how to change the url. The general form of a RewriteRule is this:
RewriteRule ^pattern$ /replacement options
The main useful option you may need to use is the [R] option, which generates an HTTP redirect header, so the user will know that they have been redirected and the new URL will show up in the address bar. This is most useful for occasions where you have moved content and you want any old links to point to the new content
This is an excerpt from the .htaccess file for this site
RewriteEngine on
RewriteRule ^$ /index.html
RewriteRule ^(.*)\.html$ /getpage.php?page=$1
The first rule will redirect anyone who goes to davw.nfshost.com with no page specified to the index page. Normally on websites this is not required to be performed explicity, but in this case I want it reprocessed by the next rule. The second rule searches for any reference to an html page and rewrites it as parameter to a dynamic script. Apache will return the results of the script and the user will be none the wiser
If, for example I wanted anyone visiting davw.nfshost.com/tutorials to be redirected to the homepage (as all of the site is tutorials), I could use the following to trigger an HTTP redirect:
RewriteRule ^tutorials/?$ /index.html [R]
Note how the [R] modifier means it will redirect the user instead of just returning different content
Guidelines for use
Having written all of this, I feel it necessary to say something about whether or not you should use mod_rewrite for a particular situation.
In general, people expect HTML pages to be static, so if you rewrote a search result URL so it looked like an HTML file people could easily be confused. Just think about it logically, if your content is just a page of information then it's acceptable to rewrite the URL of the dynamic script that's rendering it; if your page is being is being dynamically generated you should think carefully before implementing something like this.
Example: wikipedia.org
I think Wikipedia is the perfect example of how to use mod_rewrite to make usage simpler. You can access any article on the English Wikipedia with the url http://en.wikipedia.org/wiki/Article_Title which is clearly not the path of the script rendering it. However, as the articles on Wikipedia are essentially pages of information, it makes sense to do it like this so people can go to or link directly to pages in a logical fashion.