Welcome to Techno Smarter QA, Question, Answer forum. Ask questions, receive answers, or help someone. Join Now
Welcome to Techno Smarter Q&A, where you can ask questions and receive answers from other members of the community. Ask the Technical & Programming related questions that you want. Join our community and help others .
+2 votes
141 views
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.
in PHP by (210 points) 2 9
Just remove .php using .htaccess and never use any parameter in your URLs.

3 Answers

+2 votes

You can create SEO friendly URL 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

https://technosmarter.com/php/how-to-create-seo-friendly-url-for-blog-posts-in-php

by (7k points) 9 27 62
edited by
Ok thanks for reply
+1 vote

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 URL 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).  

<?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;

}

?>

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 slug function. 

Like - 

titleSlug=slug($pageTitle); 

Use titleSlug with the query and insert into the database table 

The page title will be converted into a slug .  

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

Now, you can fetch and display unique data using 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 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. 

by (8.9k points) 10 23 62
+1 vote

You can create the seo friendly url usin g .htaccess file. 

PHP function to create SEO Friendly URL

<?php
function generateSeoURL($string$wordLimit 0){
    $separator '-';
    
    if($wordLimit != 0){
        $wordArr explode(' '$string);
        $string implode(' 'array_slice($wordArr0$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));
}
by (160 points) 2
Can you please more describe it ?
I want to create SEO friendly URLs with htaccess code .
Please elaborate with steps .
...