Creating SEO friendly URLs with PHP and SQL

Creating SEO friendly URLs with PHP and SQL
Creating custom URL or SEO friendly URLs are important if you have a dynamic website and want to get more exposure in search engine results. If you use any CMS service like WordPress, Blogger or Drupal etc they provide service in their CMS to create custom URLs but if you manage your website by yourself and don't have any plugin or service to create custom URLs then you can create your own.
When you want to change the URL from the original dynamic URL your server will be accessing the original dynamic URL but rewrite a custom URL for users which you will specify. So first of all to let server rewrite URLs you need to enable mod_rewrite in Apache settings.
Now you will create .htaccess file in root directory. Many programmers rewrite all the URLs in .htaccess file with some basic knowledge but mostly users found it confusing and complicated. We will use .htaccess only to redirect all the URLs to one PHP page and then rewrite all the URLs from that page with PHP and SQL.

Create URL Table in Database

blogId INT (11),
url TEXT,

Add URL in table

Now where you are inserting your blog or article into database table at same time submit title of your blog in url field of url table. For example if you are submitting your blog with field name Add blog and we consider blogId as id of blog now you can use your blog title as url or add another field and use that for your URL. Now our function becomes like this
if($submit == "Add Blog") {
//Add rest of fields in your blog table
//Our URL will be in lower case and add dashes instead of spaces, colon marks etc
	$title = str_replace(" ","-",strtolower($title));
	$title = str_replace('"','',strtolower($title));
	$title = str_replace("'","",strtolower($title));
	$title = str_replace(" & ","-",strtolower($title));
	$title = str_replace("&","-",strtolower($title));
	$title = str_replace(" ? ","-",strtolower($title));
	$title = str_replace("?","-",strtolower($title));
	$title = str_replace(":","",strtolower($title));
	$title = str_replace(",","",strtolower($title));
    mysqli_query("insert into url(blogId ,url) values('$id','$title')");

Create .htaccess file

You have added your blog in blog table of database and added URL in url table with blogid. Now we will create .htaccess file and add following lines.
RewriteEngine On
RewriteBase /rootDirectoryName

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ URLRedirect.php [L]
In this file we write some conditions. REQUEST_FILENAME determines full local file path or script matching request.
-f is regular file treats string as pathname and tests if it exists.
-d is directory treats string as pathname and tests if it exists.
Then redirects it to the page URLRedirect.php

Creating URLRedirect.php

Create PHP page, name it URLRedrect.php and save it in the root directory. Add following lines in page.
$requesturi = $_SERVER['REQUEST_URI'];

$urlinitial = explode("?",$requesturi); 
$requesturi = $urlinitial[0];

$sql = mysqli_query("select * from urls where url = $requesturi ");
$bid = $row['blogId'];
if($bid) {

Access URL form web

Now we will access this URL form web page. Where you want to give URL add anchor tag and in href call the following function
accessURL($blogid , "blogdetail.php");
We will write function for accessURL()
function accessURL($id , $url){
    $sql = mysqli_query("select * from urls where blogId = $id ");
    return $row['url'];

comments powered by Disqus