In this series of articles we’re going to take a look at mod_rewrite, its possibilities and offer a great deal of sample snips, for those copy and paste kitties out there. Most users will learn many functions that will come in handy more times then one, once they learn and understand how to use them properly.
Let’s start by understand mod_rewrite and making sure that we have it installed and working correctly.
We first must figure out the route that needs to be taken to figure out if mod_rewrite is installed and working properly.
If root access is available and access to the httpd.conf file is available, then follow the steps outlined in “Root Access”.
For those that do not have access to root, or have no clue what I’m referring to, then please follow the direction listed under the “Shared Users” section.
Root Access Users
SSH into root, and locate the httpd.conf file.
Most users can use the locate tool, type “locate httpd.conf”.
If you’re unable to locate the httpd.conf file, try to update your server’s slocate database by using “updatedb”.
Warning: This will take a moment to complete. This is basically making an index of all files located on your server, so they can be easily found with the “locate” command.
Once we’ve located the httpd.conf file, use the locate command; if pico is being using it’s “ctrl w” and type in mod_rewrite.so. Once we’ve located this inside our httpd.conf file, we need to determine if it’s commented out or not. If there is a “#” in front of this line of code, then it’s considered commented out, if there is no “#” then we’re good to go.
mod_rewrite is current installed and running on your system.
Shared Users
For those that do not currently have access to root and can’t manually check if mod_rewrite is installed, we can do two things. We can contact our host and ask them to check, or we can run a simply test to check if mod_rewrite is installed and working.
For those opting to run the simple test, follow these directions.
1: Create two HTML files, one that will output working and another that will say isn’t working, or whatever you’d like to two to say.
2: Create an .htaccess file, if there isn’t on already installed and add this code into it.
RewriteEngine On
RewriteRule ^test.html$ /test2.html [R=301]
Now we can upload the new .htaccess file to our server and try to load the test.html file, which will produce the not working text. If apache automatically produces the working text, then everything is working correctly and mod_rewrite is installed and working correctly.
Now that we’ve determined if mod_rewrite is installed or not we can move on to what exactly mod_rewrite is.
So What Exactly is mod_rewrite?
“mod_rewrite is an extension module of the Apache web server software which allows for "rewriting" of URLs on-the-fly. Rewrite rules use regular expressions to parse the requested URL from the client, and translate it into a different URL before interpretation.”
Probable the best description I’ve yet to see descript mod_rewrite, would be the one from the Apache Reference: mod_rewrite (http://www.apacheref.com/ref/mod_rewrite.html).
“mod_rewrite is the Swiss Army Knife of URL manipulations. It provides virtually all of the functions one would ever need to manipulate URLs, and its functionality is highly generalized. Consequently, mod_rewrite can be used to solve all sorts of URL-based problems. The drawback is the high learning curve, because this module is based on a complex rule-based matching engine, which uses regular expressions for its patterns. Although the flexibility of mod_rewrite makes it a very complex tool, once you understand the basic idea you will master all existing and forthcoming URL-based problems in your webmaster's life.”
So what’s does all this really mean?
Basically all of this means is that mod_rewrite can be used for just anything URL based, which is actually more then most webmasters give it credit for.
In the next few articles we’ll go over sample scripts and techniques that can be used for many different applications and uses.
