Advanced Navigation II using Clean Urls

This tutorial will teach you how to implement clean SEO friendly web URLs.
Example: Turning /index.php?page=register into http://www.domain.com/register/.

First things first, lets explain what SEO friendly web URLs can do.

  1. They promote traffic as /home/ is easier to remember than index.php?page=home
  2. They are easier for search engines to crawl, some search engines refuse to crawl anything after the ?
  3. They are easier to maintain and keep permanent (even if you update your scripts).
  4. They hide the chosen programming language from the visiting users.

All you require is an Apache webserver (common among webhosts) & a basic knowledge of PHP.
Lets take a look at my first example, create a file called ‘.htaccess‘ (period then htaccess):

# These hashes are comments, they will explain what I am doing.
# First things first, lets turn on the Rewrite Engine
RewriteEngine On
# Then lets define some rules
RewriteRule ^([a-z0-9]+)/$ /index.php?page=$1 [NC,L]

The brackets/parenthesis are used to start a variable, variables are stored in order and are numeric, this is used and referred to later in the Rule using a $1. The [a-z0-9] signifies it can be any alphabetic or numeric character, and the plus+ symbol means it has to be more than one character long. I used a-z0-9 instead of a global (.*) which matches anything because I consider it more secure.

The ^ and $ are a standard regular expression and signify beginning and end, so the user cannot add anything else, (aka. /home/blaaaa would be invalid) and the index.php?page=$1 points back to index.php. At the end of the line, parameters or options are defined. In this case NC stands for Case InSensItIvE and the L stands for Last Rule, so if that rule is triggered, no others are.

This example turns http://www.domain.com/downloads/ into http://www.domain.com/index.php?page=downloads.
If you would like to read up on index.php?page= methods, please review “Advanced Navigation using Includes“.

Other resources I found useful:

Hope you enjoyed this tutorial.

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)