Please note, this is a STATIC archive of website technosmarter.com from July 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

How to create SEO friendly URL for Blog posts in PHP? | Part 2


In this tutorial, we will create SEO friendly URLs for the blog CMS using PHP and MYSQL database. We are elaborating "how to create a blog from beginning in PHP". Before this, we have created a simple blog in Blog Tutorial. In this tutorial, you will learn to create an SEO friendly URL by changing the same blog PHP code. In the present time , SEO is a very important technique, with the help of which blogs can be ranked in any search engine. The URL for SEO should also be SEO friendly. SEO friendly URLs for blogs can be easily created by PHP programming.SEO friendly URLs contain keywords. like

-Blog-title

How to make SEO friendly URL?

URL -

how-to-make-seo-friendly-url

In this way, you can see that title keywords are also available in the blog blog post URL.

One column has to be added to the MYSQL table to create SEO friendly URLs via PHP.


Add a database table column for SEO friendly URL –


Add a column in techno_blog table –

SEO friendly URL for blog post -MYSQL Slug Table



In the table above, you can see that the articleSlug column has been added to it, which will be used to save the slug of the blog article title to the articleSlug column and display it in the article URL. The table named techno_blog has been created in the previous tutorial. The articleSlug column has been added to create SEO friendly URLs via PHP and MYSQL. Some changes have to be made to create SEO friendly URLs.
Let's change the PHP code –


Admin/add-blog-article.php

This PHP file is used to write blog posts. Until now, blog posts were being added with IDs. But now we will insert slug in a database table instead of articleId.In order to create SEO friendly URLs, blog posts title are converted to slugs and inserted into database table. To convert blog post title to slug we will also create a function that will convert blog title to slug.
Open the add-blog-article.php PHP file and change the code –


Admin/add-blog-article.php

$stmt = $db->prepare('INSERT INTO techno_blog (articleTitle,articleDescrip,articleContent,articleDate) VALUES (:articleTitle, :articleDescrip, :articleContent, :articleDate)') ;
  



$stmt->execute(array(
    ':articleTitle' => $articleTitle,
    ':articleDescrip' => $articleDescrip,
    ':articleContent' => $articleContent,
    ':articleDate' => date('Y-m-d H:i:s'),
    
));
 

Change the code above to –
 $articleSlug = slug($articleTitle);

    //insert into database
   $stmt = $db->prepare('INSERT INTO techno_blog (articleTitle,articleSlug,articleDescrip,articleContent,articleDate) VALUES (:articleTitle, :articleSlug, :articleDescrip, :articleContent, :articleDate)') ;

$stmt->execute(array(
    ':articleTitle' => $articleTitle,
    ':articleSlug' => $articleSlug,//Blog article slug 
    ':articleDescrip' => $articleDescrip,
    ':articleContent' => $articleContent,
    ':articleDate' => date('Y-m-d H:i:s')
  
));

Understand the code above. We added the articleSlug with MYSQL insert query .


Now, we will create the slug() function file in the includes folder outside the Admin folder.
The slug() function will be converted the blog post title to slug-like –


My title is –
How to make blog in PHP?
Slug is –
How-to-make-blog-in-php

Let's create the functions.php file inside the includes folder –


admin/includes/functions.php

<?php
function slug($text){ 

  // replace non letter or digits by -
  $text = preg_replace('~[^\\pL\d]+~u', '-', $text);

  // trim
  $text = trim($text, '-');

  // transliterate
  $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

  // lowercase
  $text = strtolower($text);

  // remove unwanted characters
  $text = preg_replace('~[^-\w]+~', '', $text);

  if (empty($text))
  {
    return 'n-a';
  }

  return $text;
}

?>

 


In the PHP code above, we created a slug() function to convert the blog post title into a slug. In the code above, we used strtolower() function to convert the capital words into small later. We used another function to convert the title into SEO friendly.

Now, open the config file and include the function.php . Include the functions.php file after line 43 For eg - After the code line -
$user = new User($db); 


admin/includes/config.php

include("functions.php"); 


admin/edit-blog-article.php


Every blog post can be edited. Here , we will edit the blog post slug manually. An admin can manage the slug manually. Now change the edit file code –


admin/edit-blog-article.php


$stmt = $db->prepare('UPDATE techno_blog SET articleTitle = :articleTitle,  articleDescrip = :articleDescrip, articleContent = :articleContent WHERE articleId = :articleId') ;
$stmt->execute(array(
    ':articleTitle' => $articleTitle,
    ':articleDescrip' => $articleDescrip,
    ':articleContent' => $articleContent,
    ':articleId' => $articleId,
  
));
 

Change the code above to –

 $stmt = $db->prepare('UPDATE techno_blog SET articleTitle = :articleTitle, articleSlug = :articleSlug, articleDescrip = :articleDescrip, articleContent = :articleContent WHERE articleId = :articleId') ;
$stmt->execute(array(
    ':articleTitle' => $articleTitle,
    ':articleSlug' => $articleSlug,
    ':articleDescrip' => $articleDescrip,
    ':articleContent' => $articleContent,
    ':articleId' => $articleId
));

We added the postSlug with update query. The postSlug will be update manually because we can make manage short URL for better SEO. It will be helpful for SEO friendly URL. The blog title could be lengthily in size but the URL should not be lengthily.
Now , change the select query .
$stmt = $db->prepare('SELECT articleId,articleTitle, articleDescrip, articleContent FROM techno_blog WHERE articleId = :articleId') ; 

Change the code above to –

$stmt = $db->prepare('SELECT articleId, articleSlug,articleTitle, articleDescrip, articleContent FROM techno_blog WHERE articleId = :articleId') ; 

Here , we are selecting the articleSlug from the table. We will display the articleSlug into the text box .


Now , add a text box for articleSlug manual customization .

 <h2><label>Article Slug(Manual Customize)</label>
<input type='text' name='articleSlug' style="width:100%;height:40px" value='<?php echo $row['articleSlug'];?>'></h2>


Add the code above after the Article Title text box in the form.
You can edit the slug manually. The manual operation is important for SEO friendly URL.

blog/index.php


This is the home page file of the blog in which the list of blog articles has been displayed. Along with the blog list, a button has also been created by clicking on the blog post to read it completely. But here blog posts have been displayed by IDs, which is not SEO friendly. To make a blog post SEO friendly, the blog post will have to be displayed by slug instead of the ID, only then the BLOG post URL becomes SEO friendly. For this, we have to make some changes in the index and show PHP .


blog/index.php

 $stmt = $db->query('SELECT articleId, articleTitle,articleDescrip, articleDate FROM techno_blog ORDER BY articleId DESC');

Change the query above to –
$stmt = $db->query('SELECT articleId, articleTitle, articleSlug, articleDescrip, articleDate FROM techno_blog ORDER BY articleId DESC'); 


We fetched the blog articleSlug and will be used for the backend link for the title and read more button.
Now , change the title code –

echo '

<a href="show.php?id='.$row['articleId'].'">'.$row['articleTitle'].'</a>

';

Change the PHP code above to –
 echo '

<a href="'.$row['articleSlug'].'">'.$row['articleTitle'].'</a>

';
articleSlug.
Now, change the button backend link –
echo '

<button class="readbtn"><a href="show.php?id='.$row['articleId'].'">Read More</a></button>

';

Change to –
 echo '

<button class="readbtn"><a href="'.$row['articleSlug'].'">Read More</a></button>

';

This is the backend link for read more button .
Now , we create the .htaccess file to make redirection . We are hiding the articleId and redirecting to articleSlug using .htaccess file .


Create a file and save as .htaccess


blog/.htaccess

RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ show.php?id=$1 [QSA,L]
 
Blog is a folder name . You can remove the blog if you upload the all files on website root folder rather than blog folder –
Now, change the show.php file –

blog/show.php

 $stmt = $db->prepare('SELECT articleId,articleDescrip,articleTitle, articleContent, articleDate FROM techno_blog WHERE articleId = :articleId');
$stmt->execute(array(':articleId' => $_GET['id']));

Change the code lines above to –

 $stmt = $db->prepare('SELECT articleId,articleDescrip, articleSlug ,articleTitle, articleContent, articleDate FROM techno_blog WHERE articleSlug = :articleSlug');
$stmt->execute(array(':articleSlug' => $_GET['id']));

We fetch the articleSlug using MYSQL select query and change the articleId to articleSlug.
Now, run the PHP scripts and you will get the SEO friendly URL with your BLOG posts.


Please Share

Recommended Posts:-

Previous Posts:-

Featured Item


Complete Blog CMS system in PHP with MYSQL database | PHP scripts

$13



Modern blog CMS in PHP with MYSQL database | PHP blog scripts

$67




3 Comments
Kenoly 17 May

It is not redirecting.


Reply

@Kenoly replied by Vishal Rana 12 hours ago

Kindly remove the code line from .htaccess file

RewriteBase /blog/

Reply

@Kenoly replied by Techno Smarter 18 May

Kindly use the htaccess file code and set your source path in line.

RewriteBase /blog/

Here, the blog is the folder name where all sources are available. You have to create show.php and click from the index page of the blog website. 


Reply