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.
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)
);
- 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 |
| |
| include('db.php'); |
| |
| if(isset($_POST['create-post'])) |
| { |
| |
| $title=mysqli_real_escape_string($con,$_POST["title"]); |
| $content=mysqli_real_escape_string($con,$_POST["content"]); |
| $title=htmlentities($title); |
| $content=htmlentities($content); |
| |
| //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); |
| |
| // sql query for inserting data into database |
| $sql_query = "INSERT INTO blog_posts (title,content,str) VALUES ('$title','$content','$str')"; |
| $result_set=mysqli_query($con,$sql_query); |
| |
| // Redirect to Public post page |
| //Replace http://localhost/$str with your Greeting WEB APP URL |
| header("Location: http://localhost/$str"); |
| exit(); |
| |
| } |
| |
| ?> |
|
|
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="utf-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| <meta name="viewport" content="width=device-width, initial-scale=1"> |
| <?php $current_page = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
| echo '<link rel="canonical" href="'.$current_page.'" />'; ?> |
|
|
| <!-- Seo Meta Tags --> |
| <title>SEO Friendly URL</title> |
| <meta name="description" content="Create an SEO Friendly URL in PHP."/> |
|
|
| </head> |
| <body> |
| <br /> |
| <br /> |
|
|
|
|
| <h2 class="no-margin text-center">Create Post</h2> |
| <div class="clearfix"> </div> |
| <form method="post"> |
| <input type="text" name="title" placeholder="Your Post title" > |
| <textarea name="content" placeholder="Your post content" ></textarea> |
| <div class="form-group"> |
| <button type="submit" name="create-post" >Publish Now</button> |
| </div> |
| </form> |
|
|
| <div class="clearfix"> </div> |
|
|
| </body> |
| </html> |
|
|
- 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
| <?php |
| |
| include("db.php"); |
| |
| $str=mysqli_real_escape_string($con,$_GET["str"]); |
| |
| //Get data's from the Table |
| if(isset($str)) |
| { |
| |
| $sql_query="SELECT * FROM blog_posts WHERE str='$str'"; |
| $result_set=mysqli_query($con,$sql_query) or die('error'); |
| $user_posts=mysqli_fetch_array($result_set); |
| $title=$user_posts['title']; |
| $blogpost=$user_posts['content']; |
| |
| } |
| |
| ?> |
|
|
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="utf-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| <meta name="viewport" content="width=device-width, initial-scale=1"> |
| <?php $current_page = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
| echo '<link rel="canonical" href="'.$current_page.'" />'; ?> |
|
|
|
|
| <!-- Seo Meta Tags --> |
| <title><?php echo $title; ?> Wishing your happy independence day</title> |
| <meta name="description" content="Create an SEO Friendly URL From PHP."/> |
|
|
|
|
|
|
|
|
| </head> |
| <body> |
| <br /> |
| <br /> |
|
|
| <p class="no-margin text-center"><?php echo $title; ?></p> |
| <hr> |
| <br /> |
|
|
| <p class="no-margin text-center">Hello <?php echo $blogpost; ?></p> |
| <br /> |
|
|
|
|
|
|
|
|
| </body> |
| </html> |
- .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.
Comments
Post a Comment