User friendly URL with dynamic product name and id

Asked

Viewed 525 times

1

I am working with site friendly Urls developed in PHP. I created the file . htaccess and did the appropriate redirects according to my needs. For example, the page named.com/contact.php is now named.com/contact . So far so good. Now I have a page called noticias.php that brings several news to the user. When you click on a certain news, it is called the file mostranoticia.php? id=2 (for example id of the news 2) and the news is displayed. I want to make sure that when I click on the news, its url is named.com/mostranoticia/nome-da-noticia-selecionada/4. How can I create a rule for this and how is the link in href="" of my code?

My doubt 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 the url I cannot have these spaces.

  • Post an example of what you’ve done to improve understanding.

1 answer

0

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

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

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

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

Browser other questions tagged

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