URL Friendly: How to insert a php variable into a URL

Asked

Viewed 210 times

-2

I’m working on a PHP ad system for autonomous professionals where I’m having a hard time including the title of this ad within the page URL making it a friendly URL. I found some similar issues, but unfortunately I could not resolve the issue:

  1. Passing variables through the URL using friendly URL via GET

  2. Automatic PHP friendly URL

  3. Friendly URL with variable parameter

In this case, currently the URL of the ad page is so:

Current URL:

https://meusite.com.br/anuncio?id=10

And I would like the URL to be as follows below where after / is inserted the value of a variable in php:

URL desired:

https://meusite.com.br/anuncio/titulo-do-anuncio-aqui

To create a link to redirect the user to the ad page, insert a variable called $Row['id'] inside a href as below:

<a href="anuncio?id='.$row['id'].'">'.$row['tituloAnuncio'].'</a>

As the link tag above, the $Row variable comes from a related while a query select that runs correctly within the page.

Finally, I tried to configure Htaccess as follows below, but because I am a beginner in this area, I could not properly configure as well as find a correct way to configure the desired URL:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^anuncio\/?([0-9]+)\/?(.+)\/?$ anuncio.php?id=$1

In the case how do I use the value of the variable $Row['titloAnuncio'] inside the URL of the ad page as below?

https://meusite.com.br/anuncio/professor-particular-ou-online
  • I don’t quite understand... Your link uses $row['id'] which I suppose is numerical and wants the URL to be using $row['tituloAnuncio']? If anyone accesses https://meusite.com.br/anuncio?id=10 you want a redirect to be made or you want the URL to point directly to the title?

  • 1

    Thanks for the feedback @Papacharlie. The idea would be this same if possible. In the case currently the page is accessed by this url format https://meusite.com.br/anuncio?id=10 where the id is identified as $row[id], but the idea would be that the url would look like this https://meusite.com.br/anuncio/titulo-do-anuncio-aqui, but I don’t know how to do this configuration as much as in htaccess or php.

1 answer

1


Hello,

What is happening is the following: the url apache received is not compatible with the regular expression of the rule you passed. That’s why she’s expecting a text that contains: 1) the site; 2) the 'ad' directory'; 3) a number; and 4) a text.

You are passing: 1) the site; 2) the 'advertising' directory; and 3) a text

That said, I suggest two solutions to your problem:

The first and simplest would be to pass the ID along with the title in the URL, for example:

https://meusite.com.br/anuncio/10/professor-particular-ou-online

This already works with your rewrite rule (below), but you are not passing the ID. You can test here

RewriteRule ^anuncio\/?([0-9]+)\/?(.+)\/?$ anuncio.php?id=$1

The other alternative, that fits the suggested URL, would you map to the PHP script decide how a title relates to an id.

For this, you would need to modify the rewrite rule (leaving it the way you suggested) and can test here:

RewriteRule ^anuncio\/?(.+)\/?$ anuncio.php?titulo=$1

This would work, but you wouldn’t have the ID when you got to the page advertising.php and yes the title, so you’ll need to create a strategy to search for the title and not the ID. In this case, you need to ensure that there will be no duplicated or empty titles, otherwise your script will not work properly.

  • 1

    Thank you very much Marcos. I followed your guidance and managed to adapt my application. Thanks!

Browser other questions tagged

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