Tuesday, April 26, 2011

Mfarhanonline:SEO Friendly URLs with PHP

Fav Tag:
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.
SEO Friendly URLs with PHP


Database
Sample database blog table columns id, title, body and url.

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

Publish.php
Contains PHP code. Converting title text to friendly url formate and storing into blogtable.

<?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=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=”">
Title:
<input type=”text” name=”title”/>
Body:
<textarea name=”body”></textarea>
<input type=”submit” value=” Publish ”/>
</form>

Article.php
Contains HTML and PHP code. Displaying content from blog table.

<?php
include(‘db.php’);
if($_GET['url'])
{
$url=mysql_real_escape_string($_GET['url']);
$url=$url.’.html’; //Friendly URL
$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
<body>
<?php
if($count)
{
echo ”<h1>$title</h1><div class=’body’>$body</div>”;
}
else
{
echo ”<h1>404 Page.</h1>”;
}
?>
</body>

.htaccess
URL rewriting file. Redirecting original URL 9lessons.info/article.php?url=test.htmlto 9lessons.info/test.html

RewriteEngine On

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

Share and Enjoy: Print Digg Sphinn del.icio.us Facebook Mixx Google Bookmarks Blogplay Add to favorites BarraPunto Bitacoras.com BlinkList blogmarks Blogosphere News blogtercimlap connotea Current Design Float Diggita Diigo DotNetKicks DZone eKudos email Fark Faves Fleck FriendFeed FSDaily Global Grind Gwar HackerNews Haohao HealthRanker HelloTxt Hemidemi Hyves Identi.ca IndianPad Internetmedia Kirtsy laaik.it LaTafanera LinkaGoGo LinkArena LinkedIn Linkter Live Meneame MisterWong MisterWong.DE MOB MSN Reporter muti MyShare MySpace N4G Netvibes Netvouz NewsVine NuJIJ PDF Ping.fm Posterous Propeller QQ书签 Ratimarks Rec6 Reddit RSS Scoopeo Segnalo SheToldMe Simpy Slashdot Socialogs SphereIt StumbleUpon Suggest to Techmeme via Twitter Technorati ThisNext Tipd Tumblr Twitter Upnews viadeo FR Webnews.de Webride Wikio Wikio FR Wikio IT Wists Wykop Xerpi Yahoo! Bookmarks Yahoo! Buzz Yigg 豆瓣 豆瓣九点

http://www.mfarhanonline.com/2011042623976/seo-friendly-urls-with-php/

0 comments :

Popular Posts