Friendly URL with dynamic product name and id

1

I'm working with friendly URLs in a site developed in PHP. I created the .htaccess file and made the necessary redirects according to my needs. For example, the page namesite.com/contact.php is now as nomedosite.com/contact. So far so good. Now I have a page called noticias.php that brings various news to the user. When clicking on a certain news, it is called the file demostraanoticia.php? Id = 2 (for example id of news item 2) and the news is displayed. I want to make when clicking on the news, her url is nomedosite.com/mostranoticia/selected-name-n4/4. How can I create a rule for this and how is the link in the href="" from my code?

My question is the syntax of the regular expression and how to make the name stay in the url, since I have the name saved in the database and the name of the news has spaces between the words and in the url I can not have these spaces.

    
asked by anonymous 26.04.2016 / 18:55

1 answer

0

Since the site is still in development, you can only create a column in the database with the URL value already converted. Just having the need to use the code below only at the time of publication of the news.

preg_replace('<\W+>', "-", $Url)

The above code causes anything other than a letter or number to be replaced by a dash.

I recommend leaving the default function of the file shownoticia.php only to identify the id and if you do not have the name of the news the page redirect page with header(Location: http://www.exemplo.com/ to the link complete, making it easy for the visitor to remember the news, or to save the url.

    
26.04.2016 / 20:42