Browse By

Apache mod_rewrite Basics

Apache mod_rewrite is a tool that I wish I had tried to understand a lot early than I did. It makes URLs look so much cleaner not to mention the positive SEO results. This capability can take a URL that is common in PHP that looks like: and turn it into a URL that looks like:

The basics are fairly easy and straight forward. You will need to create a .htaccess file in the directory you want to format the URL. I always include in the first line of the file “RewriteEngine on”. You can use “RewriteEngine off” to disable blocks of rules so that you don’t have to comment them out or delete them from your .htaccess file. The RewriteRule directive uses Regular Expressions to match the URL typed to the URL you want.


RewriteEngine on
RewriteRule ^my/account([/]*)$ myacct.php
RewriteRule ^news/([0-9]+)$ news.php?id=$1


RewriteRule ^my/account([/]*)$ myacct.php: This rule looks for a URL starting and ending with “my/account” after the website, for instance and then changes it on the server side to be, but the user only sees

RewriteRule ^news/([0-9]+)$ news.php?id=$1: This rule looks for a URL starting with “news” after the website. After “news” it looks for a number that corresponds with the id generated with the news posting. The “+” allows for one or more digit to be entered. For instance would be changed on the serve to , but the user only sees

I hope this helps get you started. This is just a basic tutorial and there is so much more that can be done with Apache’s mod_rewrite feature. For more in depth information, check out these resources:

%d bloggers like this: