How to Create an SEO Friendly URL Using PHP and MySQL


SEO Friendly URL Using PHP

SEO Friendly URL Using PHP

How to Create an SEO Friendly URL Using PHP and MySQL

In this Tutorial, we are going to see a Method to Create an SEO Friendly URL Using PHP and MySQL

We are using PHP/MYSQL on this Method For SEO Friendly URL Conversion also using an HTACCESS Rewrite Rule for Convert a Dynamic URL into Clean SEO URL

Search Engines Loves Clean and SEO Friendly URLS It will Improve the user experience and Rankings.

Moz Explain the Importance of SEO Friendly URLS  – SEO Best Practices for Structuring URLs

we are already Implement this Method on our Three plugins

OK Let’s Come to our Tutorial part 🙂

How to create an SEO Friendly URL in PHP?

 

Download My Setup File


  • First, create Database Tables for URL Conversion
  • Run this Below SQL Queries to create Tables on Database
CREATE TABLE blog_posts
(
 id INT NOT NULL AUTO_INCREMENT,
 title VARCHAR (500) NOT NULL UNIQUE, 
 content TEXT,
 str VARCHAR (500) NOT NULL UNIQUE,
PRIMARY KEY (ID)
);

SEO Friendly URL Using PHP

  • Now create a File for Database Connection db.php
<?php

$dbhost = 'localhost';
$dbuser = 'YOUR DB USER NAME';
$dbpass = 'YOUR DB PASS';
$dbname = 'YOUR DB NAME';

$con=mysqli_connect($dbhost, $dbuser, $dbpass,$dbname) //connect to the database server
or die ("Could not connect to mysql because ".mysqli_error());

mysqli_select_db($con,$dbname) //select the database
or die ("Could not select to mysql because ".mysqli_error());

?>

  • index.php – Now create a Publish page it contains an HTML form for creating a post Content

 

  • PHP Script Using for SEO Friendly URL Conversion
//friendly URL conversion
 function to_prety_url($str){
 if($str !== mb_convert_encoding( mb_convert_encoding($str, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32') )
 $str = mb_convert_encoding($str, 'UTF-8', mb_detect_encoding($str));
 $str = htmlentities($str, ENT_NOQUOTES, 'UTF-8');
 $str = preg_replace('`&([a-z]{1,2})(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i', '1', $str);
 $str = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8');
 $str = preg_replace(array('`[^a-z0-9]`i','`[-]+`'), '-', $str);
 $str = strtolower( trim($str, '-') );
 return $str;
 }
 $str=to_prety_url($title);
  • posts.php – Now create  a Page for Display the Posts with SEO Friendly URL

 

  • .htaccess – Now create an HTACCESS Rewrite Rule for Clean SEO Friendly URL Redirection

= Normal URL – allwebtuts.com/posts.php?str=santhosh-veer-blog

= SEO Friendly URL – allwebtuts.com/santhosh-veer-blog

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

From the Editor’s Desk

If you Have any Doubts in this Tutorial Drop your Comments Here I Will Guide you.

 



Was this article helpful?
Thanks!

Your feedback helps us improve Allwebtuts.com