Web Design & Development

An Introduction to SEF URLs

First of all what are SEF URLs? SEF stands for Search Engine Friendly and URL stands for Uniform Resource Locator (this is what you type into your browser to find a web site e.g. http://www.google.com/.

Examples of SEF URLs

An example of a SEF URL would something like the example below:

http://www.myshop.com/products/books/frankenstein.html

Whilst an unSEF URL would be something like:

http://www.myshop.com/products.php?type=45&itemid=4234

Why SEF URLs are Important

From looking at the second URL you would not know what we were shopping for, but using the search engine friendly URLs you could see we were looking for a book, and in particular Frankenstein. By having keyword rich URLs you are helping the search engine become lazy, and search engines love it when they can be lazy.

The URLs also let people who browsing through a page of search results find relivant pages quicker. For example if you searched for “Book Frankenstein”  and the two given URLs above were listed which one are you more likely to choose, the one with “books” and “Frankenstein” in it or “products.php?type=45&itemid=4234″?

Most if not all Search Engines now provide a line showing what the URL is that the result point to. In a Google results page, the resulting URL is in green, with the keywords you searched for bolded. In the below example i searched for “Swansea” and “Shopping”. Notice how swansea is highlighted and also shopping in the URL.

Google Search Result Page highlighting SEF URLs

Making your URLs Friendly

So now you know what SEF URLs I will now introduce you to creating your own SEF URLs. Before we carry on I am working in a LAMP (Linux, Apache, MySQL and PHP) environment with Mod ReWrite Enabled (this is. So if you are not running Apache or PHP this example will not work for you.

The way I go about creating SEF URLs is as follows. I have a page which loads dynamic content, lets call it page.php

The contents of page.php are as follows:

<?php /**

*   File:       page.php

*   @author     Jon Hurlock

*   @version    1.0

*

*   The page will print out a message, followed by a content items id.

*

*   @return     the dynamic content item’s id

*/
// print out a the following line of text and GET the content’s id

echo You wish to load content with an id of #.$_GET[content_id];
    /*

* if i loaded page.php?content_id=5, it should print out:

* You wish to load content with an id of #5

*/
?>

 

We also need another file for the SEF URLs to work, this file is named “.htaccess”. Please notice the full-stop or period before the htaccess, the contents of this file are:

# Make sure the apache rewrite ,odule is on  

RewriteEngine On
# The following line says to translate a-nice-story.html to /page.php?content_id=123

# So when ever a-nice-story.html is loaded, actually load /page.php?content_id=123 but

# keep the url as a-nice-story.html

RewriteRule ^a-nice-story\.html$ /page.php?content_id=123

 

As you can see page.php prints out the following message ‘You wish to load content with an id of #99′ where 99 is passed to the page through a GET request. So if we loaded page.php?content_id=123 you would see ‘You wish to load content with an id of #123′.

Now by using the .htaccess file we are saying if a user navigates to a-nice-story.html it will load the content as if page.php?content_id=123 was loaded, however the URL which is displayed is a-nice-story.html

The line beginning RewriteRule in the file .htaccess uses regular expressions to match SEF URLs to the ‘true’ URL.

URL Rewriting Conventions

Most URLs that are re-written follow a simple convention. Only the alpha numeric characters are used except for en-dashes (e.g. -), underscores (e.g. _) and full stops / periods (e.g. .). However the fullstop /.period is only used if you want to give your re-written URL an extension such as .html or .php etc. I have createn a simple SEF URL Generator ti see if your generated URLs conform to the standard. It is written in jQuery. just start typing in the input box to see if it conforms. If the box turns green it is ok, if it turns red it doesnt follow the convention.


Tags: , , , , , , ,

Leave a Reply