First of all, let me start by confessing that I’m not an expert in .htaccess modification. This is something I’ve learnt and I will guarantee that there will thousands of developers who know this topic like the back of their hand. Therefore this little tutorial is for those guys who may have been searching for simply ages for an answer that doesn’t seem to be out there. So please, if you’re an experienced programmer, please do me the liberty of leaving out all the usual flaming stuff that can be found on the likes of YouTube. Instead, please take the liberty of contacting me directly. With that said, let’s move on.
Well finally after a good few days searching solid for how to do it, I’ve done it. What am I talking about? Well, what seems like a basic PHP design tactic these days is tidying up your user’s address bar by using mod_rewrite on Apache. Not only that, but it also makes site design, as well as link management and organisation much, much easier.
I’ll give you an example, if you don’t know what I’m on about. Have you ever been to a site and noticed that in the address bar you see a link that resembles http://acmeinc.com/products or http://someblog.com/archives? The main thing to notice here is that usually people are used to http://acmeinc.com/products.html or http://someblog.com/archives.php - however this isn’t happening in this mod_rewrite technique. It looks much tidier, and makes maintaining the site a lot easier.
What’s going on under the bonnet?
First I need to explain exactly what is happening behind the scenes for those who don’t know. Apache, the server technology of a site, is receiving requests for web addresses, as you’d expect. If a user navigates to http://someblog.com, Apache will automatically look for the default page (usually something along the lines of index.html index.php), and dish it back out to the user. Let’s take a practical example. Consider Facebook - which dishes you out http://www.facebook.com/home.php. All good, but it’s ugly (at least by modern web standards).
Now with this little technique which you’re about to learn, things change. Mod_rewrite modifies the requests by “rewriting” the request that Apache receives from a user’s browser. The mod_rewrite rule I use appends “index.php” to a request if what’s requested isn’t found. Now you know what’s happening behind the scenes, let’s move onto how you can apply this to the structure of the site.
Applying this to a site
Ok, so what we discussed above seems very pointless at first, and usually people struggle to think how they can apply this to benefit their site. However, Wordpress is a good example of how people use it to help them. The technique I use is organising pages into folders that resemble what you would usually use as page names. Considering my site, I have several main sections. Let’s take my portfolio as an example. Usually, you may decide to use “portfolio.php”, and include this in your navigation however in my case things aren’t working this way. I actually have a folder called portfolio in the root of my server, and within that folder, an “index.php” file containing all the juicy content you would originally expect in the “portfolio.php” file.
Can you seen what happens here? So all my links will point simply to “/portfolio” (yes really, view my source) instead of “/portfolio.php”, which means Apache will get a request for http://www.george-edwards.co.uk/portfolio and deal with it by appending “index.php” onto the end.
You should now start to see things taking shape. This theory doesn’t work too greatly with a WordPress driven site (I find the file structure of WordPress pretty messy), but if you use it for developing your own site, you’re going to have a very tidy looking site structure which makes development in a text editor surprisingly simple.
Here’s an example. Consider you’re making a simple site for “Apache Software Ltd.”. Apache (like the name?) may want a typical four page setup - Products, Contact, About and News. Nothing unusual here. But let’s think about the directory structure (click the picture and look at our example in motion):

Here you can see it in action. Your navigation would be very simple in this case. You may use a PHP include to grab navigation elements from the header.php file in the include folder. But the case remains the same - your links would all be along the line of “/products”, “/contact”, “/about” and “/news”. The same effect happens in the address bar. Your user would be looking at “http://apachesoftware.com/products” and they would never see the ugly “index.php” on the end. Imagine just how simple it is to manage, having everything in folders like that!
Modifying .htaccess to add the rewrite rule
One thing is left. We need to re-do the htaccess file. Simply add the following to the end of the file and restart your Apache service:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Enjoy. Any thoughts? Let me know via the comment feature.