How Make SEO Friendly URLs with PHP

Among the very fundamental SEO techniques is in using Search Engine Optimization (SEO) friendly URLs constructions. An excellent construction will assist the indexation procedure for your web site, but, sadly, there are a lot of sites that use wrong, URLs that are best. In this short article we have a look in we are able to develop a construction that can help us rank as highly as you possibly can and the different components of a URL.

They may find yourself indexing just a portion of your website 's pages, causing a number of its own content if what they see is mistaking.
SEO Friendly URLs with PHP

Essentially, you had need to:
  1. Let search engines understand which of the pages of your site's needs to be crawled
  2. Produce a map of your website 's construction to tell if a few of your pages may be obtained via multiple URLs.
  3. When the Search Engine Optimization specialist is called in too late within a website 's development procedure, added challenges arise. Within my experience, web developers and SEOs have differing views on what an SEO-friendly URL is.
E.g., from the web designer's point of view, the URL - http://www.example.com/forum/viewtopic/t-121638.html- is entirely normal. While from an SEO's perspective, the next variation http://www.forum.example.com/section/topic-name.html- would be considerably more purposeful.

Make SEO Friendly URLs with PHP

Follow below codes to Make SEO Friendly URLs with PHP:

Database

Create database with columns id, title, body and url.
CREATE TABLE blog
(
id INT PRIMARY KEY AUTO_INCREMENT,
title TEXT UNIQUE,
body TEXT,
url TEXT UNIQUE,
);

Publish Page

Use following code for publish title text to friendly url format.
<?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 Page

Use article.php for display content from database 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.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+).html$ article.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+).html/$ article.php?url=$1

Database Configuration

Use your database details and add into db.php file for database configuration:
<?php
$mysql_hostname = "localhost";
$mysql_user = "username";
$mysql_password = "password";
$mysql_database = "database";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
?>

Download Script 

Download script and config & upload it on your main directory:

SEO Friendly URLs

Related Posts