Sexy URLs
Does your site contain database driven content? Do you wish your URLs were memorable, user hackable, SEO friendly and more secure? In this article I’ll show you how you can turn you old query string URLs into Sexy URLs. All it takes is a little Apache ForceType and some PHP!
Before We Start
First off, let me say that this isn't a new technique, its one that has been around for years, but it's one I often see sites not taking advantage of.
Note: I'll be using our Add-ons section as an example throughout this article.
The Good, The Bad and The Sexy
I first started looking in to Sexy URLs over a year ago when we decided to re-vamp our RapidWeaver Add-ons section. We set a few goals for the new Add-ons section, one of the main ones was to generate user friendly URLs: we wanted users to be able to easily remember or guess the URLs for each product type, individual product, vendor or any combination of the three. So I set about investigating how we could do this and came to the conclusion that using Apache's ForceType along with PHP was the best solution.
The Good
The main advantages of using this technique is so you can give your visitors URLs they can easily remember, improving your sites SEO and making your site feel more professional.
The Bad
We have all seen URLs that contain query strings, they're ugly and SEO un-frindly (although search engines have got better over the years with indexing query string URLs). On the old Realmac Software site we were guilty of using ugly URLs. For example, if you were browsing through the Add-ons for themes, you would have been served up a URL along the lines of;
http://www.realmacsoftware.com/addons/index.php?start=25&type=theme&rw_version=
This URL is obviously passing a string that PHP will query the database with. In this case the results will start at item 25, where the type of product is a theme and is also available for any RapidWeaver version. The technique is bad for many reasons:
- It’s not user friendly: It's virtually impossible for a user to remember the URL without bookmarking it.
- Bad SEO: Search engines find it difficult to interpret URLs containing query strings, making it harder to index your pages.
- Unprofessional: Your URLs look messy and unprofessional.
The Sexy
With this in mind I set about generating Sexy URLs. These were URLs our visitors could easily remember and hack together. Finding the same results as the query string based URL above now looks like this:
http://www.realmacsoftware.com/addons/themes/3/
The URL is clean, clear and concise. Visitors can easily read and remember this URL. Search engines can easily identify the content within, meaning improved indexing of your site.
The ForceType Directive
To implement Sexy URLs we need to use Apache's ForceType Directive. This will allow us to tell the server to treat a file as PHP even though it doesn't have a .php extension. The first thing you need to do is create a .htaccess file in the directory your PHP script will reside. The .htaccess file will tell your server that you want the file named addons to be treated as if it were a PHP file with a .php extension. To do this add the following code to the file:
<Files addons>
ForceType application/x-httpd-php
</Files>
Next add a file named addons (without an extension) to the same directory as the .htaccess file. You can now add PHP code the addons file as if it were called addons.php, go ahead and try to echo out some PHP :)
The PHP Code
The next step is to be able to get some variables from the URL that PHP can query the database with. To do this we need to get the URL that is being requested, to do this we'll use the global $_SERVER['PATH_INFO'] variable and PHP's explode function. The $_SERVER['PATH_INFO'] gets the server path being requested and the explode function allows us to split that information into an array.
Add the following code to your addons file:
$url_array = explode("/",$_SERVER['PATH_INFO']);
echo "<pre>";
print_r($url_array);
echo "</pre>";
If we pass a URL of http://www.yourdomain.com/addons/themes/3/ to your addons page you will see the following:
Array
(
[0] =>
[1] => themes
[2] => 3
[3] =>
)
You can now construct an SQL query based upon the URL array. For example:
$query = "SELECT * FROM my_table WHERE products = $url_array[1]";
It's that simple! You can now start constructing your SQL queries from the $url_array, you should add some logic to your PHP to work out the type of variable that has been passes to it (product type, page number, product name, etc) and query your database appropriately. Keep in mind how you will deal with malicious users trying to trick your script: always make sure you are escaping any quotes, slashes and strange characters for added security.
Going Further
You can of-course expand on this as I have for the Add-ons section of our site. For example, I've added some functionality that replaces spaces with a "-" in product and company names. When I grab the URL I replace the "-" with a space so we can query the database correctly.
Hack My URLs
You can take it even further and add some logic to your PHP and guess what you think your user is looking for. Try constructing your own URL on our Add-ons section. I've put in place some PHP that will guess what you're looking for (products, company names, product type, etc). For example, if you wanted to find a business theme you could simply enter:
http://www.realmacsoftware.com/addons/themes/business/
you will be given a list of all the business related themes. However if you enter:
http://www.realmacsoftware.com/addons/business-media/
you will be shown information about one of our themes because the database has returned a unique result!
Thanks for reading, if you have any questions or feel I've missed out some information, drop me a comment below.
