Replace part of the URL with text

Asked

Viewed 179 times

1

  • vc ta using wordpress?

  • try to rename the file containing the page from "a" to my test-article , and instead of making a call GET Voce has to do POST

  • I’m not using wordpress, I blogged with PHP.. this page a.php is called when clicking on an article, I could not see the name change to the article name.

1 answer

0

The system you want is called slug.

Slug

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

In this example, I am creating a function that gets the title of the article and converts 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, other functions can be added: strtolower or improve the pattern to avoid special characters.

The function must be used in the link, which will call the page. I recommend that you add before Slug, a parameter for the ID, as "site.com.br/1293/o-mundo-vai-acabar-em-2016

Pros

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

Cons

Unless you use the parameter ID before Slug, you should have some attention

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

Sources for the department

How can I simplify Urls for a website?

bramus/router

Browser other questions tagged

You are not signed in. Login or sign up in order to post.