How many times have you visited (or perhaps made) a website, in which the URL’s become a long query string? The number is countless, without a doubt. URL rewriting is a very overlooked technology when it comes to developing database driven websites. So much of the users query is transmitted to the script in the form of domain.com/bookstore.php?subject=physics&author=hawking, etc But, by implementing URL rewriting, that URL can be changed into domain.com/bookstore/physics/hawking.
There are a couple of advantages to doing this. Search engines are hesitant to browse pages with long query strings. With the proper use of robots.txt, having these engines browse your dynamically generated pages won’t put a large strain on your server, and will drive even more visits to your website. The second reason you may want to switch is so that people can actually verbally communicate the URL. Some may not believe that this is a big plus, but try to tell a friend who’s never been to slashdot.org how to get there. It has also been argued that URL rewriting is a more secure way of doing things, as it doesn’t expose file extensions or the ways in which you query a database.
Basic Rewriting
Here’s the scenario: You own an online software store, and it’s divided into two parts, one devoted to Mac software and the other for PC software. As of right now, when a user clicks on a link to take them to the PC section, they are routed to the following URL: domain.com/display_software.php?os=1
Where 1 pulls up all the PC software from the database, and 2 displays Mac products. To the programmer, this may look fine, because the script name shows what the script does, and 1 or 2 are related to items in a database. But, not everyone is a programmer. And making websites as user-friendly as possible is essential.
The solution is to somehow get the user from domain.com/software/1/ to the PC software section. And the road to achieving the solution is mod_rewrite.
Open up a text editor, and create a file named “.htaccess”. Inside that file, write the following:
RewriteEngine on RewriteRule ^software/([0-9])/$ display_software.php?os=$1 Don’t be alarmed if you didn’t understand the second line, we’ll get to that. Open up your FTP program, and upload this file to your document root folder (which is typically, public_html/). Now, in your browser go to yourdomain.com/software/1/ and, it will be redirected on the server level to the display_software.php file. If you want to see the information you passed into the query string, then use the code below and upload it as the missing document. <? echo $_SERVER['QUERY_STRING'] ?>
If you reload the page, you’ll see “os=1”, which is exactly what we wanted. Change around the number after “software” and play with it a bit. Unless you have a wider knowledge of regular expressions, you won’t feel very comfortable experimenting with this technology. In response to this, the second part of this article will contain a small overview of how to use regular expressions, as well a more advanced URL rewriting techniques.
Web Host Auditor is an independent web hosting review web site. We invite you to send us a review of your current or past hosting company to help us in our rankings of hosts on this site.
Web Host Auditor is an independent web hosting review web site. We rate hosting companies based on past user feedbacks, lab benchmark testing, and our usage. We invite you to suggest a host for ranking on this site or submit a review of your host.