Replace part of the URL with text

1

I created a blog and want to replace part of the URL, when an article is opened, by the title of the article.

link

I want to replace the part a.php?a=3&v=0 with the title, it would be

link

    
asked by anonymous 08.06.2018 / 16:37

1 answer

0

The system you want is called slug .

Slug

To work with Slug, it's easy, but it depends on some settings, such as URL amigável .

In this example, I'm creating a function that gets the title of the article and converts it to slug

<?php
function slug($title){
   $slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $title);
   return $slug;
}
echo slug('O mundo vai acabar em 2016'); //O-mundo-vai-acabar-em-2016'
?>

The function is very basic, you can add other functions: strtolower or improve pattern to avoid special characters.

The must function be used in the link, which will call the page. I recommend that you add before the slug, a parameter for ID , such as "site.com.br/1293/o-world-windows-in-2016"

Pros

The url will be theoretically clean for both the search platforms and the end user.

Cons

Unless you use the ID parameter before the slug, you should have some attentions

  • Revert the slug to the original form and deal to receive the rest of the contents of the database.
  • Consider that the title is unique, not to give conflicts

Supplies for depots

How can I simplify URLs for a website?

bramus / router

    
08.06.2018 / 17:02