One of the common things that comes up in web design forums is ugly URLs. For instance index.php?section=paintings&id=25 is an example of a GET query string (from the question mark and on). The variable "section" is set to "paintings" and the variable "id" is set to "25." This is great for beginners of PHP, but it is not good for search engines. Google and others often look at one or two variables in the query, but even that isn't always the case. The best answer is to rewrite the URLs.

This very simple tutorial assumes that your host has already compiled Apache with rewrite options.

You should figure out how you want to organize your URLs before you get too far, especially if you have a lot of variables to worry about. Normally, you use the broadest category down to the most specific. For instance: "paintings/2006/August/29/" would be recommended over the reverse (though you can pretty much do it however you want).

For our example, we are going to convert index.php?section=paintings&id=25 to paintings/25.php. There are two variables that we care about (section and id). So we want to take the value of section and put it in the first slot, add a forward slash, add the value of id, and then add a php extension. Here's the code that would be in the ".htaccess" file (this is normally in the root of your web directory, but you may need to create it):

CODE:
  1. RewriteEngine On
  2. RewriteBase /
  3. RewriteRule ^(.*)/(.*).php index.php?section=$1&id=$2

The first line turns the RewriteEngine on (at least that is pretty self-explanatory!). Be sure you pay attention to case (capitalized vs. lowercase). The second line sets the base URL (so the root directory is used as the starting point). The third line is what determines which variables go where. RewriteRule simply says: "Hey, a URL rewriting rule follows." The carot (^) starts the regular expression (RegEx), but you don't need to fully understand that to use rewrites. The "(.*)" is a wildcard, which means it will match any string of characters, numbers, or dashes and place it in the first variable. Then we have a slash and another wildcard, which is placed in the second variable. Finally we have the ".php" extension (you don't even need an extension if you don't want one!).

Since you didn't change your index file to operate any differently, the data still needs to be passed to it in the same way it would have been. That's where the second part of the RewriteRule comes in. You should recognize it, but the $1 and $2 are different. These are the variables we grabbed from the wildcards earlier on the same line.

When visitors go to your site, it will look like you have a "paintings" folder and a numbered file for every single one, but you really have a single file handling those variables. Now instead of this:

CODE:
  1. http://www.example.com/index.php?section=paintings&id=25

Visitors and search engines will see this:

CODE:
  1. http://www.example.com/paintings/25.php

Once you have played around with the basics, I encourage you to check out some of the more advanced rewriting features by searching around the net. There is a lot that rewrites can accomplish and this tutorial has only hit the very basics.


1 Response to “The Basics of Apache Rewrites and Friendly URL’s”

  1. 1 Josh Schumacher

    Wow, that is very cool. I’m going to have to try that out! Thanks for the brief tutorial on it and bringing my attention to this method of making urls that are a lot more search engine and user friendly.

Leave a Reply