How to make SEO friendly URLs

Making Search Engine friendly URLs for your blog or website is so easy, This post will explain you Why to make SEO friendly URLs and how to make SEO Friendly urls using .htaccess or PHP code…

What are the SEO Friendly URLs ?
As we know SEO stands for Search Engine Optimization, Now let’s understand SEO friendly URLs… Basically we have common url in our website or blog e.g. http://www.thewebdevelopers.org/index.php?page=”my%first%post” Such kind of URLs are not SEO friendly because as per Search Engine algorithm,

They only read keyword etc. and if your blog url content such a relevant keywords that people used in search e.g. http://www.thewebdevelopers.org/my-first-post/ These simple URL called SEO friendly URLs…

From an SEO point of view,  a website’s URL structure should be follow these points …

  • Straightforward: URLs with relevant keyword and URL should not be ugly that includes special characters and something like that user can not remember…
  • Meaningful: URL names should have keywords in them, So that it create some meaning of the text …

Benefits of SEO Friendly URLs :-

Friendly URLs improves your site search engines ranking. Before trying this you have to enable mod_rewrite.so module at httpd.conf. It’s simple just few lines of PHP code converting title data to clean URL format.

How to make SEO Freindly URLs ?

There is many techniques to rewrite the URL(making SEO friendly URLs ) , but I can say here is the one of the best way to making URLs SEO Friendly using some PHP code..

Let’s try..

Step 1 :  Just create a database table to store page content in that..Sample database table  for blog that contain columns id, title, body and url…

CREATE TABLE blog(id INT PRIMARY KEY AUTO_INCREMENT, title TEXT UNIQUE, body TEXT, url TEXT UNIQUE);

Step 2 :  In this step let’s create a page blog-page.php

It contains some PHP code. Converting title text to friendly url format and storing into blog table…

//including db file
include('db.php');
function string_limit_words($string, $word_limit)
{
$words = explode(' ', $string);
return implode(' ', array_slice($words, 0, $word_limit));
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$title=mysql_real_escape_string($_POST['title']);
$body=mysql_real_escape_string($_POST['body']);
$title=htmlentities($title);
$body=htmlentities($body);
$date=date("Y/m/d");

//Title to friendly URL conversion
$newtitle=string_limit_words($title, 6);

// First 6 words
$urltitle=preg_replace('/[^a-z0-9]/i',' ', $newtitle);
$newurltitle=str_replace(" ","-",$newtitle);
$url=$date.'/'.$newurltitle.'.html';
// Final URL

//Inserting values into my_blog table
mysql_query("insert into blog(title,body,url) values('$title','$body','$url')");
}
//HTML Part
<form method="post" action="">
<b>Title:</b><input type="text" name="title">
<b>Body:</b><textarea name="body"</textarea>
<input type="submit" value=" Publish "/>
</form>

Step 3 :   In this step let”s create a new file article.php that contains HTML and PHP code. We will fetch content from blog table and will display them on this page …

include('db.php');
//Friendly URL 
if($_GET['url']) { $url=mysql_real_escape_string($_GET['url']); $url=$url.'.html'; 

$sql=mysql_query("select title,body from blog where url='$url'");
$count=mysql_num_rows($sql);
$row=mysql_fetch_array($sql);
$title=$row['title'];
$body=$row['body'];
}
else
{ echo '404 Page.'; }


//HTML Part
if($count) { echo '<h1>$title</h1><div class="body">$body</div>'; }
else
{ echo '<h1>404 Page.</h1>'; }

Step 4:   In the final step .. you need to create a file and put the code given below into the .htaccess file and than upload it to your website root directory…

RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+).html$ article.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+).html/$ article.php?url=$1

That’s it !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

15 − eleven =