There are a few standard tricks I like to perform using the htaccess file on Apache servers. Rewriting URLs is a bit trickier to do than most things with htaccess, but if you can find every ugly URL in your script and change it to a ‘dynamically clean’ format, then you have a good part of the URL rewriting done.
The tricky part comes when using a specially formatted code to translate the dynamic parts of the static URLs. The first thing to remember is that you will hae to put the dynamic part of the URL into the ugly URL from before, but this old, ugly URL is ’secretly’ passed to the script. This allows your script to behave as it did before the change.
Each piece of the dynamic URL should be held within (parenthesis) and so that we can use the dynamic value in the old URL, they increment from left to right… The first set of parenthesis is $1, the second (code) is $2, and so on. We’ll need to know that for the later part of this lesson.
We need a way to set a standardized template for the ‘clean’ URLs so a URL like index.php?user=Eddy12&page=101 could eventually look like eddyison101.html
The first line of the rewriting needs to turn the engine on and look like …
RewriteEngine On
The second part of this URL rewrite should start with the term…
RewriteRule ^clean url$ old.url?goes=here
We want ^eddy12ison101.html$ but eddy is dynamic and changes for each user and the page that a user is on could be anywhere from 1 to 99999. How do we know what it will be? Here comes the special code… ^()ison().html$ OK, that’s not quite it, but did you see how I replaced the dynamic content with (parenthesis)? now we are set to have the username (eddy) as $1 and the page number as $2. Easy enough so far? Moving along…
Now, there is certain types of information that we expect to find in here so we’ll tell it what to expect. The part of the (code) that holds the username will contain letters and numbers. It would look like ([0-9a-zA-Z]) to cover lower and upper case letters and all of the numbers. This example only collects 1 such letter or number and we want the full username. Adding an asterisk* after the [braces] will allow for ‘0 or more’ of that character so it will look like ([0-9a-zA-Z]*)ison([0-9]*).html
That covers just numbers for the page number, but that is the only type of data that we expect to get there. One last change to that is to add a backslash before the .dot. in the .html It should now look like \.html in order to prevent confusion on the dot’s other meaning.
RewriteRule ^([0-9a-zA-Z]*)ison([0-9]*)\.html$ index.php?user=$1&page=$2
Hopefully, without using very technical language, you will have learned something from this and that you are now on your way to understanding regular expressions. Usually, mentioning that term makes people run, so I held off until the end. The Mod_Rewrite module allows you setup the htaccess file to show how Apache will handle URLs. This is one of the single most helpful things that you can do to optimize your site.
URL rewriting is important to do if you plan on any SEO work to be done on your site. The fact is that Dynamic URLs can prevent some spiders from visiting your site. This is an SEO essential and applies mainly to websites like stores with lots of dynamic content or any other website using a databse for forums, blogs, catalogs, news, and more. Contact Comptrio for an analysis of your link problems and get the biggest boost for the fewest bucks on your path to SEO and ranking greatness.
Tags:
We will use Mod_Rewrite in your htaccess file to present naturally search friendly urls.