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

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. 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

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 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
  • 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.

Technorati tags: SEO tips, URL rewriting, Apache, mod rewrite, rewrite urls, url rewriting

Published by

Keith Dsouza

I am the editor-in-chief and owner of Techie Buzz. I love coding and have contributed to several open source projects in the past. You can know more about me and my projects by visiting my Personal Website.I am also a social networking enthusiast and can be found active on twitter, you can follow Keith on twitter @keithdsouza. You can click on my name to visit my Google+ profile.

  • thanks for all information.

  • Sumit Joshi

    It’s a nice tutorial.

    I would like to know that how to Create SEO friendly links in php code so that when user overs the mouse on our link, he/she can see SEO friendly link. I think you got the point.

    Means that How can I create that link and php code?

    Thank you for this tutorial.

  • Very useful post. really thank you. very nice site.

  • elp

    i’m using xampp on winxp
    i’ve check my mod_rewrite on phpinfo(). it does exist. but when i try to use .htaccess to rewrite my url, it doesn’t work at all. do i miss something here?

    i did try it as your sample exactly. but it still didn’t work.
    here my testing case



    RewriteEngine On
    RewriteRule ^(.*)/(.*)/(.*).html /index.php?act=$1&id=$2&page=$3

    • elp

      it’s my mistake
      i forgot to check the error logs :D

      thanks. it’s a nice tutorial

  • nice article !!! well explained tks

  • Atara

    Very useful article.

    Note: One should consider a case that the parameter-value contains slashes. e.g productName=”name1/name2″. such case mis-lead the URL parsing

    and Question: How do you set the index of the corresponding parameters? e.g. in
    RewriteRule ^products/(.*)/(.*)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$
    what is $1, $2, $3, . . .


    • @Atara – The index is set based on the regex you use, for example (.*) corresponds to $1, (.*) to $2, ([0-9]) to $3 and so on.

  • Well written post would be immensely useful for people

  • Hey! Great, I am going to try this now. I thought I would throw you a question. I usually figure it out before a response but you could make it a lot easier for me =) I am also a self educated person, so try to explain well =)

    Recently, I started a Volusion store and I HATE how all the articles have “?” in them! In wordpress, it is VERY easy to change the URL/make it what you want to begin with. With volusion, it’s impossible. Here is an example of a URL:

    It is weird because the products and categories do have that.. just the articles. Any ways, is the code that you listed above good for the “?” ? Or is there a different one I need to add?

  • Rohan

    i can’t able to make SEO friendly URL by rewriting the rule. means it’s open the page and show content also but not open any include file means css, js files. this is in core php. can anyone help me out ?
    actually i am not understanding that need to change only in htaccess file or i have to change in php file also and if it’s then what kind of change i will make in php file ?

    my code is here,

    php files.




    font-family:Verdana, Geneva, sans-serif;

    htaccess file
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule index/view/(.*)/ index.php?view=$1
    RewriteRule index/view/(.*) index.php?view=$1

    • Atara

      You might need to add BASE tag in your php, because the URL was changed, so it cannot find the included files.

      e.g. add it after your HEAD tag:

      . . .

      • Atara

        < head >
        < base href=”” >

  • Great post can u please give some example also

  • Hi, your post was very helpful,
    i have the following in my .htaccess file

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php

    i reall need help trying to make a url like http://domain/page?id=1 get a value from the database using the id=1 look like

    thats for the anticipated response

  • thank you very much for good article.

  • vipin


    Please would you let me know that, which thing need to pass in anchor tag to url rewriting.

    Please help.



  • Ruth

    Do I need afterwards to change all my links on my site to the friendly url?