Search Engine Friendly URLS

 

I am sure you have all seen the sites that have urls that look similar to books.php?id=4353. A URL like this not very friendly to users or even search engines. No person is going to remember that book number. Along with this search engines do not always properly crawl this.

Wouldn’t books/harry-potter.html make both users and search engines happy?

You might ask how an HTML file parse PHP code in this example. Well it works by using a simple .htaccess Unix file in the appropriate folder. You only need a couple lines in this file to make it work. Below is some sample code for the basics.

RewriteEngine On
RewriteRule ^books/harry-potter.html$ books.php?id=4353

Explanation

  • Line 1: Turns on the URL Rewrite Engine
  • Line 2: First half is a regular expression of what a user enters into the browser and the second is where that is redirected.

Now this is fairly simplistic as this only work if a user types in the exact harry potter url. We can also make this Rule work for multiple books and change the parameter in the PHP file using a regular expression.

RewriteRule ^books/([a-z-]+).html$ books.php?title=$1

In the above code we are saying anything that after /books/ and before .html take that combination of letters and redirect to books.php with those values assigned to the title. The OPTIONAL parentheses allow us to use what is in between like a variable. The first parentheses are represented by $1, the second $2, etc.

Examples

  • books/my-sweet-book.html calls books.php?title=my-sweet-book
  • books/testing.html calls books.php?title=testing
  • books/asp-unleased calls 404 not found because the .html is missing

Related posts:

  1. Search Engine Positioning
  2. MAMA – Search Engine for a Web Pages Structure
  3. Part 5: Online Marketing and Search Engine Marketing
  4. Correct Mistaken URLS
  5. Review of “PHP for Absolute Beginners”


Written by Brenley Dueck

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
connect with me!