Create SEO Friendly URL’s Using Mod Rewrite and PHP – Part 1

by Keith Dsouza | Translate | Print
Monday, 22nd Oct 2007 | Share


Share Create SEO Friendly URL’s Using Mod Rewrite and PHP – Part 1 on Twitter Share Create SEO Friendly URL’s Using Mod Rewrite and PHP – Part 1 on Facebook Save Create SEO Friendly URL’s Using Mod Rewrite and PHP – Part 1 To Delicious Favorites Stumble Create SEO Friendly URL’s Using Mod Rewrite and PHP – Part 1 Share Create SEO Friendly URL’s Using Mod Rewrite and PHP – Part 1 on Digg Get Instant Updates as RSS Feeds from Techie Buzz

First of all I am not a SEO Specialist, I just play around with lots of stuff and happened to learn URL Tweaking for better SERP’s. If you are a WordPress user then you will not require to use this trick but if you are a webmaster who owns a product based site or some other site which generates dynamic content based on parameters then this trick is the one which should help you better your SEO rankings.

Let’s look at a example of a URL which does not attract much Search Engine Love.

http://www.yoursite.com/product.php?productid=2&categoryid=3. It is hard even for a human to tell what the product is by looking at this URL. With the trick we will use the final URL can look something like this http://www.yoursite.com/product/13/3/2-GB-MP3-player.html.

Now if someone looks at this link they can easily know that the product is a 2 GB MP3 player. Now if you want hire a professional SEO to do the changes it may cost you quite a lot, with a little bit of PHP knowledge and using some simple URL rewriting using Apache Mod Rewrite you can achieve this for free.

I term this simple SEO rule as What to look for and Where to go. We’ll learn more about these terms as we get ahead with the tutorial.

What is Mod Rewrite?

According to Wikipedia

A rewrite engine is a piece of web server software used to modify URLs before fetching the requested item, for a variety of purposes. This technique is known as URL rewriting. Some benefits derived from a rewrite engine are:

If you look at the definition you may have seen that it allows you to modify URLs before the requested item is fetched meaning that we will still be internally using the product.php but to human eyes and search engines it will look as a different URL altogether.

Steps to Rewrite your URLs

Ok now that we have seen what Mod Rewrite actually does we will start on learning how to actually rewrite you URL.The first step you will need to undertake is create a .htaccess file and place it in the root directory of your website. Once you have created the file using a text editor edit it and add the following lines;

RewriteEngine on
RewriteRule ^article/([0-9]+)/([0-9]+)/(.*?).html$ /article.php?articleid=$1&categoryid=$2

Lets take a look at the two lines individually.

RewriteEngine on

This line tells the web server that we will be rewriting the URL’s otherwise it may start throwing 404 errors, this line is very important before you can start creating the rewrite rules. Now lets look at the second line which is the most crucial one in actually redirecting your URLs.

With this particular line we are creating a rule telling the web server that when it encounters a URL that matches the rule it should do what we have defined. Lets decipher the rule part by part. We are using a few Regex patterns which can be easily learnt through regex online tutorials.

RewriteRule ^article/([0-9]+)/([0-9]+)/(.*?).html$ /article.php?articleid=$1&categoryid=$2

In the above line RewriteRule (a rule to rewrite the URL for the web server Apache) tells the web server that this a rule that it should follow for URL rewriting. Further the rule is divided into two parts for simplicity lets call the first part "what to look for" and the second part "where to go". So in the above line we are telling the web server what to look for and if it finds it where it should go.

Part 1 What to Look For?

The text forms the pattern of what to look for "^article/([0-9]+)/([0-9]+)/(.*?).html$". The what to look for pattern should always end with a dollar($) sign. Lets compare the pattern with the URL we are aiming to create which is http://www.yoursite.com/product/13/3/2-GB-MP3-player.html. Lets divide the pattern into different parts and compare it with our URL.

  • The first character of the pattern the exponential sign "^" signifies the start of an URL in this case http://www.yoursite.com.
  • The next part article/ relates to the first directory in the URL which ends with a slash "/".
  • Now the next section you will see something like this ([0-9]+) which a regular expression telling the web server that it can encounter any number between 0 and 9 after article/, the + sign signifies that the number can occur 1 or more times. For example your article id may be either 1, 20, 567, 2005 etc.
  • After that comes another ([0-9]+) which signifies the category id again your category id may be a single number or anything else.
  • After that we are using (.*?).html which is a wildcard saying after the last / we may have anything with any number of characters or numbers ending with a .html extension.
  • The $ lines tells the web server that this is the end of the pattern.

 

In the example we used ([0-9]+) but say your category id cannot be more than 100 then instead of using the + symbol you can tell the web server that there will be only a double digit number using [0-9][0-9]. In that case the rule would be changed as such

RewriteRule ^article/([0-9]+)/([0-9][0-9])/(.*?).html$ /article.php?articleid=$1&categoryid=$2

In the above example we looked at some not so simple regular expressions that can let the web server know that we are following a certain pattern for a URL that needs to be redirected. Though the above example looks tough, it’s not tough at all, once you get to know how to use the regular expressions.

Rewriting URL’s require you to use regular expressions and without knowing this you will have to pay the SEO guys a huge amount.

If you have any questions feel free to contact me using the contact form.

Part 2 involves making the changes in your PHP files, if you need any help don’t ponder before consulting me it will be done for free of course only if you meet the requirements where I do not need to charge you.

Do let me know your views and comments I am always waiting for it and get few of it.



Share

18 Responses so far | Share Your Opinions!

  1. Shivaranjan
    October 23rd, 2007 at 11:02 am #

    Well written post would be immensely useful for people like me who do not know much about PHP and SEO.
    Stumbled…

    Reply to this comment

    keith Reply:

    Glad you liked it Shiva.

    Reply to this comment

  2. Nirmal
    October 24th, 2007 at 2:10 am #

    Very useful post indeed. Stumbled.

    Reply to this comment

  3. Hi
    March 21st, 2008 at 2:24 pm #

    Hi,

    How can I rewrite Wordpress post names

    Where it has %20 (space) character, to replace it into -?

    Reply to this comment

    Keith Dsouza Reply:

    You can check that by going to Option -> Permalinks in your WordPress admin panel, if you want to learn more search Google for WordPress Permalinks Tutorials.

    Reply to this comment

  4. Swapnil
    March 26th, 2008 at 2:50 am #

    Hello,

    This is very helpful.
    I did get the Part 1 of the section, but from where can I get the Part 2 i.e. makin changes in the PHP files ???

    Reply to this comment

    Keith Dsouza Reply:

    Hey Swapnil lack of time led me from writing the part 2 but I will be doing it very shortly now.

    Reply to this comment

  5. Ian
    March 27th, 2008 at 5:41 am #

    Great article, will be using this shortly as I am rewriting my website and want to get rid of the variables in the address.

    Reply to this comment

    Keith Dsouza Reply:

    Glad you found it useful Ian

    Reply to this comment

  6. abhishek
    July 11th, 2008 at 1:24 am #

    good article but some what gone over head…will read it again when free

    Reply to this comment

  7. goldfries
    September 9th, 2008 at 5:15 am #

    Darn, still no part #2?

    Reply to this comment

  8. DStudioBali
    October 12th, 2008 at 10:57 pm #

    Another great post about mod rewrite. And I will rewriting my website and want to get rid of the variables in the address.

    Reply to this comment

  9. Keith Dsouza
    October 31st, 2008 at 1:10 pm #

  10. nn
    November 5th, 2008 at 1:54 am #

    This is not very useful article. I could not do anything even i know the REGEX.

    I don’t know why but the rule is not redirecting my urls and not changing them what i want.

    Bye

    Reply to this comment

  11. Berghian Alexandru
    December 17th, 2008 at 1:02 pm #

    very good work !!!

    Reply to this comment

  12. Duy
    April 23rd, 2009 at 6:05 am #

  13. Saloni
    July 20th, 2009 at 4:09 am #

    A very great help indeed.I have been searching the internet all day when I came across this post!Lucky me .

    I am also looking for contact form (as I have a question to ask )…where is the contact form?

    Reply to this comment


Trackbacks

  1. mod_rewrite and SEO URL’s, a Voyage of Self Discovery. « Tony’s Technical Blog

Leave a Comment

Note: We discourage users from using keywords in their names while posting comments, most of them get caught by spam, also it really would be more fruitful in knowing people who comment by their real name, rather than by using a name no one relates to. In future we reserve the right to delete comments from users using a name other than their own.

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>