Photosho and Web design tutorials for your inspiration
Create SEO friendly URL`s using mod rewrite in PHP (.htaccess)

In this tutorial I will show you how to convert dynamic URL`s into Search Engine Friendly and easy to understand both for humans and machines URL`s. To change the URL`s we will use the Apache`s mod_rewrite function what can easily match URL`s using regular expressions. If it sounds confusing don`t worry, I will explain everything step-by-step and you can always leave a comment!

In this tutorial I will show you how to transform this (and means the "and" sign):
http://www.mywebsite.com/tutorials.php?id=123(and)title=mod_rewrite(and)category=php
into this:
http://www.mywebsite.com/tutorials/php/mod_rewrite-123

Why to rewrite your URL`s ?

The main reason is because static URL`s (the second URL is static) are indexed more faster than dynamic URL`s (like the first one), because from a static URL is easier to understand what the page is about both for search engines and your visitors. When using dynamic URL`s there is an another problem: search engines are reading only the beginning of the long URL`s, so if your addresses are different only at the end, than search engines will see the same URL for all of your pages and from this reason none will be indexed.

How this works ?

We will use Apache mod_rewrite. The mod_rewrite transforms back the second URL into the first URL, so the server will access the "ugly" address, but the visitors and the search engines will see the "clear" one. mod_rewrite simply rewrites the URL`s what meets specific conditions into addresses what are understood by the server.

Let`s rewrite it!

The code what rewrites the website address needs to be placed in your .htaccess file (if you don`t have it in your root folder, create it), the code is very short:
RewriteEngine on
RewriteRule ^tutorials/(.*)/(.*)-([0-9] )/?$ /tutorials.php?id=$3(and)title=$2(and)category=$1

Take it apart!

The first line: RewriteEngine on simply does what it says, it starts the rewrite engine.
Now let`s take apart the second line:

RewriteRule

This tells the server about the rule to follow when rewriting the URL

^

This exponential sign means the start of the URL: http://www.mywebsite.com/

tutorials

This word is simply added to the website address

/

This character separates the directories

(.*)

This tells the server that here will be placed some data (any kind of characters) (e.g.: php - 3 letters)

-

This character will be simply added to the website address, we could use any other character of words here

([0-9])

This tells the server that here will be placed a digit (e.g: 1)

the plus sign tells the server that we accept here more data of the previous type (in our case a digit - [(0-9)]), in our case this means that there can be more digits (e.g.: 123)

?

This sign tell the server that the character in it`s front it`s not required (so the URL will work with and without the last "/")

$

The dollar sign tells the server that here is the end of the rule
The last part of the line is the old address, the value of each variable (id, title and category) are changed to $3, $2 and $1. mod_rewrite takes these variables and places them in their new position in the new URL. In the first place for data (the first (.*)) it places the $1`s value (the category value), in the second place the value of $2 and so on. If you want to change the order just change the $1, $2 and $3 order, but be careful to set the right data type in the new URL.

I know that it`s not easy, if you have any question please don`t hesitate, ASK ME! And I will try to answer as fast as I can. You can for example write me in a comment your URL and how you want to transform it and I will try to make it ;)

And don`t forget (and) stands for the "and" sign - I can`t write it because some parsing errors, If you try to write an URL in a comment than you should also change the "and" signs to (and).

Thanks for reading!
Enjoyed this Article ?
Csabi

By Csabi

5 comments so far:

LEAVE A COMMENT

Debi

Posted 198 days ago.
This artlcie went ahead and made my day.
Reply

search engine optimization

Posted 249 days ago.
Do you guys still do link building for your personal website?
Many people say to avoid that nowadays
Added to Reddit, someone will like it
Reply

Lars

Posted 277 days ago.
Naved Ahmed
I think you miss understood the article, you should read it again.
ViewMoreApps?id%3Dhigh%26q=air AND ViewMoreApps?id=high&q=air
Has nothing to do with SEO optimized url.

Csabi
some what nice article, even though i agree with your approach as you see some
do get confused a little bit.
So not to drag it out too much, but I think you could go a little more into details
of why and what is done.
Reply

Csabi

Posted 316 days ago.
I`we never seen a problem like this...

I think this is because some settings issues in Apache, I`m not sure it can be fixed with mod rewrite
Reply

Naved Ahmed

Posted 316 days ago.
Hi, I am new in php developement. I read above tutorial, but cant understand all of it.
I am trying to convert this url :
ViewMoreApps?id%3Dhigh%26q=air
into this
ViewMoreApps?id=high&q=air
How could I do it ?
Please help me.
Reply

Leave a comment! - Join the debate!

Name:
Email Address:
URL:
Your Comment:
Notify me of followup comments via email
This comment will be a reply to comment #asdasda by author Cancel Reply