User friendly URL does not retrieve variable

Asked

Viewed 589 times

3

I’m trying to make the Urls on my site friendly.

URL friendly

 http://localhost/mg/artista/10 

.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d

  RewriteRule ^/artista/([0-9]+)$ /artista.php?artista=$1
</IfModule>

PHP to retrieve variable

$artista = $_GET['artista']; 

You are giving the following error: Notice: Undefined index: artist

I saw in some places people recovering the variable using $_SERVER['REQUEST_URI']. If I use this, it returns me: /mg/artist/10. I could take the variable, but I don’t know if this is the right way.

2 answers

1

The problem with your .htaccess is that in the rule

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

the ^/ at the beginning indicates that the path must start with /artista. As in your case the path starts with mg/, the rule is skipped.

For it to work you just need to remove the ^/ from the beginning of the rule.

Ex.:

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d

  RewriteRule /artista/([0-9]+)$ /artista.php?artista=$1
</IfModule>

Note: If not already known, here you can find a very good htaccess test tool.

  • thanks for the tool tip, I didn’t know. I took the " /" and the error remained the same. Is the error not in the variable recovery? Abs

  • I saw in some places people recovering the variable using $_SERVER['REQUEST_URI']. If I use this, it returns me: /mg/artist/10. I could take the variable, but I don’t know if this is the right way.

  • @Ricardo Try to take the / before artista and see if it works for you.

  • @Andréribeiro The problem may be $ at the end of the expression it was not enough just to remove ^ from the beginning. So it seems to work: RewriteRule /artista/([0-9]+) /artista.php?artista=$1.

  • @Andréribeiro did not help to take the /

  • @Qmechanic73, also not working

  • Thinking that the error is not in . htaccess but in variable recovery...

  • @So Ricardo is some problem with his apache.

Show 3 more comments

0

Browser other questions tagged

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