Please Login or Register to continue.


I want to make SEO friendly URL in PHP I am trying to many days but unable to solve this error. When I click on news then slug change but post data not change.

(230 Points)
in PHP


3 Answers
(7.3K Points)

You can create SEO-friendly URLs with PHP and MYSQL database. If you are not getting URLs related data on the page then check your condition.GET blog posts slug and fetch data by post slug. Like this-

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

You can check the complete tutorial here


Ok thanks for reply
liveatenws 24 Apr 2020

(9.4K Points)

SEO-friendly URLs help in the ranking of blog and website pages. SEO-friendly URL contains the page content keywords. We can customize the URL according to our needs. Yes, you can create SEO-friendly URLs with PHP and MYSQL database. In PHP, we convert the page title into SLUG.SLUG is another format that is converted by PHP functions.   

Make SEO friendly URL with PHP and MYSQL database-

Title - How to make SEO friendly URL 

SLUG of title- how-to-make-seo-friendly-url 

Slug contains a minus sign between words.  

We create a slug() function in PHP using PHP builts functions. The slug() function is used to convert the title to slug (As we discussed above).  


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;




The slug() function is created with many built functions of PHP. 

If you want to create an SEO-friendly URL in PHP then you have to create another column in the table. 

Like - titleSlug 

The titleSlug column will help you insert, update the slug. 

First of all, use convert the website page title or blog post title to slug using the slug function. 

Like - 


Use titleSlug with the query and insert it into the database table 

The page title will be converted into a slug.  

In this way, you can make SEO-friendly URLs in PHP with an MYSQL database. 

Now, you can fetch and display unique data using a slug. 

You have an issue with the query. Insert the unique data in every row. Do not add the same data in every row. 

Create a query like this - 

SELECT pageTitle,pageContent from table_name WHERE titleSlug=:titleSlug 


Make sure to GET a unique titleSlug on the show page. 

It will help you fetch the unique data on-page. 

This tutorial is available here - 

Create SEO friendly URL in PHP with MYSQL database

for better understanding try to learn from the beginning 

How to create blog using PHP and MYSQL database 

These are enough to learn and create the SEO-friendly URL in PHP. 





(160 Points)

You can create the SEO-friendly URL using the .htaccess file. 

PHP function to create SEO Friendly URL

function generateSeoURL($string, $wordLimit = 0){
    $separator = '-';
    if($wordLimit != 0){
        $wordArr = explode(' ', $string);
        $string = implode(' ', array_slice($wordArr, 0, $wordLimit));

    $quoteSeparator = preg_quote($separator, '#');

    $trans = array(
        '&.+?;'                    => '',
        '[^\w\d _-]'            => '',
        '\s+'                    => $separator,
        '('.$quoteSeparator.')+'=> $separator

    $string = strip_tags($string);
    foreach ($trans as $key => $val){
        $string = preg_replace('#'.$key.'#i'.(UTF8_ENABLED ? 'u' : ''), $val, $string);

    $string = strtolower($string);

    return trim(trim($string, $separator));


Can you please more describe it ? I want to create SEO friendly URLs with htaccess code . Please elaborate with steps .
shakti 13 Aug 2020

Related Questions :-

Featured Items:-

Certificate system in PHP website | PHP scripts


Home Credito loan finance management multipurpose system in PHP


Result management system with Marksheet in PHP website | PHP Scripts


Your Answer

You can post your answer after login..