Like Us on Facebook

SEO Friendly URLs using PHP with HTACCESS

This post explains how to create SEO friendly URLs using PHP with .htaccess. This type of friendly urls improve website's seo for search engines ranking. Before you trying this you have to enable mod_rewrite in your server.It is very easy, you have to follow simple few steps. So lets start


 

The script contains HTACCESS with three PHP files.

db‚Äč.php // Database Connection File

index.php
 // Converting title to SEO friendly URL

article.php // SEO Friendly URL post Page

.HTACCESS  // Redirecting original URL to SEO FRIENDLY URL
 

 

Database

Create Table blog2
(
   id INT PRIMARY KEY AUTO_INCREMENT,
   title TEXT UNIQUE
   body TEXT,
   url TEXT UNIQUE,
);
 

db.php

<?php
    error_reporting(E_ALL && ~E_NOTICE);
    mysql_connect("localhost","root","");
    mysql_select_db("blog2");
?>
 

index.php

<?php
include('db.php');

function string_limit_words($string, $word_limit) {
   $words = explode(' ', $string);
   return implode(' ', array_slice($words, 0, $word_limit));
}

$blog='';

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);

$newtitle=string_limit_words($title, 10);
$urltitle=preg_replace('/[^a-z0-9]/i',' ', $newtitle);

$newurltitle=str_replace(" ","-",$newtitle);
$url=$newurltitle;


mysql_query("insert into seo_friendly_url(title,body,url) values('$title','$body','$url')");
$blogurl="http://localhost/seo_friendly_url/$url";


}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
 
<title>SEO Friendly URLs - UandBlog</title> 

 
<style> 
.editbox
{
display:none
}
td
{
padding:7px;
}
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}

 .shade
{
box-shadow:0px 0px 18px #000000;
-moz-box-shadow:0px 0px 18px #000000;
-webkit-box-shadow:0px 0px 18px #000000;
border-radius: 8px;-moz-border-radius: 8px; -webkit-border-radius: 8px;


</style>
 
 
</head> 
 
<body bgcolor="#dedede"> 
<div style="margin:0 auto; width:750px; padding:10px; background-color:#fff; height:800px;" class="shade"> 
<div style="margin-top:10px;"> </div> 


<h1>SEO Friendly URL using PHP - UandBlog.com demo</h1> 


<br>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{

?>
<a href="<?php echo $url; ?>"><?php echo $blogurl; ?></a>
<?php
 
}
?>
<br  />

<form method="post" action="">
<table width="100%">
<tr>
<td width="80px" valign="top">
<b>Title:</b>
</td>
<td><input type="text"  style="width:400px;border:solid 2px #006699; padding:5px" name="title"/></td>
</tr>
<tr>
<td width="100px" valign="top">
<b>Body:</b>
</td>
<td><textarea name="body" style="width:400px; height:200px; border:solid 2px #006699; padding:5px"></textarea></td>
</tr>

<tr>
<td width="100px">

</td>
<td><input type="submit"  value=" Publish "/></td>
</tr>

</table>

</form>
</div>

 
</body></html>
 

article.php

<?php
include('db.php');

if($_GET['url'])
{


$url=mysql_real_escape_string($_GET['url']);
$url=$url;
$sql=mysql_query("select title,body from seo_friendly_url where url='$url'");
$count=mysql_num_rows($sql);
$row=mysql_fetch_array($sql);
$title=$row['title'];
$body=$row['body'];

}
else
{

echo '404 Not URL Available.';
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
 
<title><?php echo $title; ?> - UandBlog</title> 

<style> 
.editbox
{
display:none
}
td
{
padding:7px;
}
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}

 .shade
{
box-shadow:0px 0px 18px #000000;
-moz-box-shadow:0px 0px 18px #000000;
-webkit-box-shadow:0px 0px 18px #000000;
border-radius: 8px;-moz-border-radius: 8px; -webkit-border-radius: 8px;



 
</style> 

 
</head> 
 
<body bgcolor="#dedede"> 
<div style="margin:0 auto; width:750px; padding:10px; background-color:#fff; height:800px;" class="shade"> 

<?php 
if($count)
{

echo "<h1>$title</h1>
<div class='body'>$body</div>";

}
else
{

echo "<h1>Not URL Available 404.</h1>";
}

?>

</div>

 
</body></html>
 

.htaccess

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

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.