Friendly URL does not retrieve variable

3

I'm trying to make my site's URLs friendly.

Friendly URL

 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 the variable

$artista = $_GET['artista']; 

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

I saw in some places the personnel recovering the variable using $ _SERVER ['REQUEST_URI']. If I use this, it returns me: / mg / artist / 10. By giving an explode I could get the variable, but I do not know if this is the correct way.

    
asked by anonymous 31.01.2015 / 02:26

2 answers

1

The problem with your .htaccess is that in the

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

^/ at the beginning indicates that path should begin with /artista . As in your case the path begins with mg/ , the rule is skipped.

For it to work you only 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 you do not know, here you can find a very good tool to test htaccess .

    
31.01.2015 / 03:16
0

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

link

    
22.03.2015 / 06:30