This is tutorial about building search engine friendly URLs with htaccess and PHP . URLs play a very important role in on page search engine optimization.Search engines rank static URLs(http://example.com/my-title.html) better than dynamic URLs(http://example.com/post.php?id=34).All dynamic URLs can be converted in static URLs easily with the help of “.htaccess” and PHP.We will try to understand these URLs rewriting techniques with the help of few examples.
1.In first example let us consider we have a website which contains URLs like
http://example.com/post.php?id=1
http://example.com/post.php?id=2
http://example.com/post.php?id=3
Our purpose is to change these URLs in following formats.
http://example.com/my-first-post-title-1.html
http://example.com/my-second-post-title-2.html
http://example.com/my-third-post-title-3.html
An easy approach is to add the following rules in your .htaccess file.
|
1 2 |
RewriteEngine on RewriteRule ^(.+)-([0-9]+)\.html$ post.php?id=$2 [PT,L,QSA] |
Above rewrite rule with pass all request where URLs end with .html and contain numbers before .html to your page post .php.In above code PT (pass through) flag used to pass request to “post.php” page ,L(last) flag will stop matching other rules after this line and QSA(query string added) flag will pass query string “id” to page “post.php”.I have “id=$2″ because Apache assign unknown values of URLs to variable like $1,$2,$3 automatically on first come first serve basis.(.+) comes first in the rule so its values is assign id in $1 and ([0-9]+) comes after it so its value is assigned in $2.
2.In second example let us consider we have URLs which contains more than one parameters like this.
http://example.com/post.php?cat=3&id=1
http://example.com/post.php?cat=2&id=2
http://example.com/post.php?cat=1&id=3
Our purpose is to change these URLs in following formats.
http://example.com/third-category-3/my-post-title-1.html
http://example.com/second-category-2/my-post-title-2.html
http://example.com/first-category-1/my-post-title-13.html
We can use following rules to .htaccess.
|
1 2 |
RewriteEngine on RewriteRule ^(.+)-([0-9]+)/(.+)-([0-9]+)\.html$ post.php?catid=$2&id=$4 [PT,L,QSA] |
In above both examples URLs contains one or more numbers.If you do not want to display these numbers in URLs you can read example 3 and 4 ,but in these situations you have to change your PHP code slightly.
3.In third example let us consider we have URLs like this.
http://example.com/post.php?id=1
http://example.com/post.php?id=2
http://example.com/post.php?id=3
Our purpose is to change these URLs in following formats.
http://example.com/my-first-topic-title.html
http://example.com/my-second-topic-title.html
http://example.com/my-third-topic-title.html
The difference between first and third example is this we are not showing any identifier in URLs.
We can use the following rewrite rule for this.
|
1 2 |
RewriteEngine on RewriteRule ^(.+)\.html$ post.php?title=$1 [PT,L,QSA] |
Remember that the above rule will not pass “id” parameter to “post.php” page.Instead of “id” it will pass “title” to page “post.php”.We can retrieve the value of ID by adding the following code to your page “post.php” at beginning.
|
1 2 3 4 5 6 |
<?php $title=$_GET['title']; $q="select id from post where title='".$title."'"; $r=mysql_query($q); $res=mysql_fetch_array($r); $id=$res['id']; |
4.In fourth example let us consider we have URLs which contains more than one parameters like this.
http://example.com/post.php?cat=3&id=1
http://example.com/post.php?cat=2&id=2
http://example.com/post.php?cat=1&id=3
Our purpose is to change these URLs in following formats.
http://example.com/first-category/my-first-post-title.html
http://example.com/second-category/my-second-post-title.html
http://example.com/third-category/my-third-post-title-.html
We can use following Rewrite rules to .htaccess.
|
1 2 |
RewriteEngine on RewriteRule ^(.+)/(.+)\.html$ post.php?category=$1&title=$2 [PT,L,QSA] |
Like third example here we can retrieve the paramers “catid” and “id” using following PHP code.
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $title=$_GET['title']; $category=$_GET['category']; $q="select id from post where title='".$title."'"; $r=mysql_query($q); $res=mysql_fetch_array($r); $id=$res['id']; $q="select id from categories where category='".$category."'"; $r=mysql_query($q); $res=mysql_fetch_array($r); $catid=$res['id']; |
Incoming Search Terms:
- htaccess friendly url example
- seo friendly urls php mysql example download
- urls

