Note: This article assumes you have a basic understanding as to what .htaccess files
are, how they work and how to create one. And that you know how to use regular expressions.
When we were green programmers, we would create a website with these files:
index.php about.php contact.php
And when our visitors wanted to access a certain web page within our website, we would
provide them with links to these documents directly like this:
However, all this above turned out to be the wrong way for many reasons. First of all,
referencing web pages with their full name is not very secure because an evil user would know
(a where "about.php" is located, b) that it is a PHP file and that will facilitate his work to ruin our website.
The correct way to fix this is with a .htaccess file placed within out root directory where our website resides.
Before you use your .htaccess file to re-write an URL, make sure your server a) supports .htaccess files because some
bosses of hostings forbid them for God knows what reasons and b) that even if the hosting allows .htaccess files, you might
need to contact the hosting people to ask them to configure their server to accept URL re-write on a per directory basis.
Now we would want to turn this URL http://www.example.com/about.php into http://www.example.com/about
Our very first directive must be:
RewriteEngine On # We are turning on the rewrite engine on our server
Some hostings require you to specify as to from where the re-writing shall begin.
Let's say you store all of your website files and folders in your root directory.
Then your second directive will be:
RewriteBase / # We shall be re-writing files in our root directory.
When testing on localhost using the "RewriteBase" directive will give a server error.
Our third directive:
RewriteRule about about.php
Now, golden rule - when re-writing a URL, you must first specify the RewriteRule directive and then
the word right after that is the word that rewrites the name of the file whose name you want
to re-write. So RewriteRule about means that the word that will come after "about" is the one
that will henceforth be known as "about" in this case about.php
RewriteRule about about.php # Re-write the file name about.php to about
So when we write in the URL http://www.example.com/about the server will actually be pulling the information
from about.php but the user will never know! That will be our little secret!
Now the problem with the above approach is that the re-writed name is not spesific. Meanning that "about"
will match any URL that has the word "about" in, for example:
The above will be matched. We do not want that. We can solve this problem with regular expressions:
RewriteRule ^about/?$ about.php
Super! Now our URL will only match http://www.example.com/about with or without trailing slash, does not matter.
One last thing - we want about URL to be case-insensitive so that http://www.example.com/about and http://www.example.com/AboUt will both
match the about.php file. We do that by putting the [NC] flag at the end of the directive like so:
RewriteRule ^about/?$ about.php [NC]
"NC" meanning "no case". I also thought it meant "North Carolina" but sadly it did not.
All directives put together:
RewriteEngine On RewriteBase / RewriteRule ^about/?$ about.php [NC]