×
Register
 

Article

Seo Friendly Url with PHP and htaccess

(2788 views)

Seo Friendly Url with PHP and htaccess

This post explains how to create SEO friendly URL with dynamic content using PHP and .htaccess mod redirection. 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.

 

Live Demo            Download  

 

Database

The first thing create a database called friendly_url with blog table columns id, title, body and url.

CREATE TABLE `blog` (
  `id` int(10) NOT NULL,
  `title` text NOT NULL,
  `body` text NOT NULL,
  `url` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

Create a db.php file

Connect to the MySQL Database by using mysqli_connect() function.

<?php
$con=mysqli_connect("localhost","root","","friendly_url");
if(mysqli_errno($con)){
echo "Database connection failed: ". mysqli_error();
}
?>

 

Create a publish.php file

Contains PHP code. Converting title text to friendly url formate and storing into blog table.

<!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</title> 
 
<link href="style.css"> 
 
</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>

 <?php
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=mysqli_real_escape_string($con,$_POST['title']);
$body=mysqli_real_escape_string($con,$_POST['body']);
$title=htmlentities($title);
$body=htmlentities($body);
$date=date("Y/m/d");

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

$newurltitle=str_replace(" ","-",$newtitle);
$url=$date.'/'.$newurltitle.'.html';


mysqli_query($con,"insert into blog(title,body,url) values('$title','$body','$url')");
}

?>
<?php 
// tui them vao khuc nay
$r=mysqli_query($con, "select * from blog ");
while($row=mysqli_fetch_array($r)){
?>
<h2><a href="<?php echo $row['url'];?>"><?php echo $row['title'];?></a></h2>
<?php } ?>


<h1>SEO Friendly URLs with PHP</h1> 

<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
Contains HTML and PHP code. Displaying content from blog table.

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

if($_GET['url'])
{

$url=mysqli_real_escape_string($con,$_GET['url']);
$url=$url.'.html';
$sql=mysqli_query($con,"select title,body from blog where url='$url'");
$count=mysqli_num_rows($sql);
$row=mysqli_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; ?></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>

 

Create a .htaccess file

RewriteEngine On

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