mod_rewrite is a popular Apache Module which
gives a server administrator the ability to mask a web site's links.
Though mod_rewrite has several abilities and can be used in
several ways, the only way focused on by this tutorial is to create
Search Engine Friendly links.
This tutorial is divided into several sections to help you get started
quickly. If you do not have access to your Apache Configuration file or
you do not know what an Apache Configuration file is, you should skip
the first section. If you encounter any errors at the end of this
tutorial, the first section would be a safe bet to review.
Setting up Apache
Browse to your Apache directory and open up the Configuration file,
usually named httpd.conf, for both Apache 1.x and Apache 2.x.
For Apache 1.x, the file will usually be located inside the base
directory of the Apache installation and for Apache 2.x, it will
usually be located inside a directory called /conf. If you are
using some sort of control panel software, consult your manual.
Scroll down in the Configuration file until you find a line reading:
Now change it to make it look like this, by removing
the comment:
Basically what this does is make Apache load the
mod_rewrite module. It comes bundled automatically with all versions of
Apache 1.x and Apache 2.x so there is probably little or no chance you
won't find it.
Most web servers do not need the following step, might you might
want to follow it to avoid any errors. If you decide to skip this Step
and later encounter, errors, you might want to go back to it.
Scroll down in the Configuration file until you find the section
reading:
# This controls which options the .htaccess files in
directories can
# override. Can also be "All", or any combination of "Options",
"FileInfo",
# "AuthConfig", and "Limit"
#
AllowOverride All |
If the last line, the one that says AllowOverride
All is set to anything else, change it to read AllowOverride
All exactly as shown above. Apache some times has a problem giving
access to HTACCESS files on a web server, so this Step ensures that
proper access to the HTACCESS file will be given at all times.
We are now done ensuring Apache will work with mod_rewrite. If
you have skipped ahead from the start of this tutorial, do not worry
about the previous steps. If you do not have access to your Apache
Configuration file because you're hosted on a remote server, again do
not worry. If your host provider is even half decent, they would have
automatically completed the previous Steps for you.
Creating the HTACCESS File
Create a new file named .htaccess. If you are working on a
Windows machine, Windows will usually not allow you to create a file
with only an extension, such as in our case. The only solution is to
login to your web server via FTP and create the file on your web server
via the FTP connection and then copy it back to your Windows machine.
Open the created .htaccess file and write the following lines
into it. It is preferable to open the file with Notepad if you're
working on a Windows machine since Notepad does not add any extra
characters to a file when opened:
RewriteEngine On
RewriteRule ^profile/([0-9]).html$ /viewProfile.php?id=$1 |
The first line, RewriteEngine On, is
basically the command to Apache to turn on the rewrite engine. We are
basically telling Apache that rewrite rules are about to follow.
The second line, RewriteRule ^profile/([0-9]).html$
/viewProfile.php?id=$1 , is our actual rule. Basically what this
means is that when Apache encounters a link in the form of say profile/1.html,
it should consider it the same as a link in the form of viewProfile.php?id=1.
Confused? Let's narrow down the code just a little:
| RewriteRule ^profile/([0-9]).html$
/viewProfile.php?id=$1 |
The bolded part of the code is what we want Apache
to expect. In this case, we are saying we want a link in the form of <a
href="profile/1.html">. Where did we get 1.html you
might be wondering? It is here :
| RewriteRule ^profile/([0-9]).html$
/viewProfile.php?id=$1 |
mod_rewrite uses Regular Expressions. A
Regular Expression is simply A set of characters, meta characters, and
operators that define a string or group of strings in a search pattern.
In other words, if we are searching for a string but do not know the
exact characters that make it up, we can use Regular Expression to
define a pattern, or an idea of what type of characters make it up, so
we can find it. This part here:
| RewriteRule ^profile/([0-9]).html$
/viewProfile.php?id=$1 |
The bold text is the actual pattern we are looking
for. In this case, we know the URL starts with profile/ and
even though we do not know the rest, we know it will be any character
that makes up a digit between 0 and 9.
We just set up our pattern. Now whenever Apache encounters a link such
as <a href="profile/1.html"> or <a
href="profile/15.html">, it will translate it.
Translate What!?!
You might be wondering what exactly constitutes a 'translation'. Let's
display the previous code and review what translation means for mod_rewrite.
| RewriteRule ^profile/([0-9]).html$ /viewProfile.php?id=$1 |
You see the bold text? That is our PHP script. When
Apache encounters a link, as stated before, it will translate it to be
the same as viewProfile.php. In other words, this link format, <a
href="profile/15.html"> is the same as this format, <a
href="viewProfile.php?id=15">
We just hid away completely the fact that we are using PHP, and still
maintained our benefits. You might be wondering what the $1 at
the end means:
| RewriteRule ^profile/([0-9]).html$ /viewProfile.php?id=$1 |
Now we are simply telling Apache to provide the
result of that pattern as the id for our script. $1
simply means the first pattern. If we had a second pattern, it would be
$2, a third would be $3, and so on. Here is a visual
example:
| RewriteRule ^profile/([0-9])/([0-9]).html$
/viewProfile.php?id=$1&page=$2 |
Final Testing
Finally, let's test what we have done so far. We already created the
.htaccess file, so now let's create a simple PHP script called viewProfile.php
to test what we have done.
Paste the following PHP code into a blank file, save it as viewProfile.php,
and upload it to your web server in the same directory as the .htaccess
file. This will be explained later.
<?php
// Get ID.
$_id = $_GET[ 'id' ];
// Print Yes if ID is greater than 10,
// or No if ID is less than 10.
if( $_id > 10 ) {
echo 'Yes';
} else {
echo 'No';
}
?> |
Create another HTML file, call it whatever you want,
paste the following HTML in it, and upload it to your web server in the
same directory as the PHP script and the .htaccess file.
<a href="profile/9.html">Test 1</a>
<a href="profile/11.html">Test 2</a>
<a href="profile/1.html">Test 3</a> |
After you have uploaded all three files, point your
browser to the HTML file and try clicking on any of the 3 links. Can
you guess which of Yes or No will be displayed for each
link. If nothing worked, then you have a problem and should go back to
the previous steps and make sure you did everything correctly.
More Regular Expressions
mod_rewrite is powerful and you can bet that the pattern we used
in our example is not the only one available. You can use unlimited
amount of patterns and define different ones but this is outside the
scope of this tutorial. One of the most common patterns that you might
use however is this:
| RewriteRule ^profile/(.*).html$ /somefile.php?id=$1 |
The (.*) simply means any combination of characters.
You might be wondering what the ^ and $ at the end of each link means:
| RewriteRule ^profile/(.*).html$ /somefile.php?id=$1 |
These are the characters for line start and line
end, respectively. You always start a new pattern with ^. You do not
always need to end the line with $ but it is good practice otherwise
Apache will always attempt to match characters even after those you
specify.
| RewriteRule ^profile/(.*).html /somefile.php?id=$1 |
i.e. Without the $, a link like profile/alex.htmllalal
or profile/meAhe.htmlalAOlAO103 will always work for the above
pattern.
The most and last important to thing to point out is that we have made
sure that your web site contains links that will be easier for Search
Engines to follow and index. The only problem now is that you have to
worry about manually re-coding all your hyperlinks!
|