hide variable in url with htaccess

3

I wonder if there is any way to hide a subdirectory or a variable in the friendly url? Example:

.htaccess

RewriteRule ^([a-z,0-9,A-Z,_-]+)/categoria-([a-z,0-9,A-Z,_-]+)$ index.php?link=6&categoriaid=$1

In this case, I pass the category id via url that would look like this:

www.com.br/19/categoria-teste 

In this case it works ok, but what I really want is to hide the ide from the category for the user and it shows

www.com.br/categoria-teste

But of course, passing the id of the variable hidden.

    
asked by anonymous 08.11.2016 / 20:22

2 answers

6

First I recommend you learn , yours is wrong ( and it works by "luck"):

[a-z,0-9,A-Z,-]

Within [...] do not use comma to separate, all values are detected, the correct would be something like:

RewriteRule ^(\d+)/categoria-([a-z0-9A-Z_\-]+)$ index.php?link=6&categoriaid=$1

Since the ID must be numeric, I have changed by \d

You can not hide the id that is between /<id>/categoria-<nome> , see the stackoverflow URLs themselves:

http://pt.stackoverflow.com/questions/164248/esconder-variavel-na-url-com-htaccess
http://pt.stackoverflow.com/questions/<id>/<nome>

Maybe you can use it like this:

http://site/categoria-teste-19
http://site/categoria-<nome>-<id>

Regex would look like this:

RewriteRule ^/categoria-([a-z0-9A-Z_\-]+)\-(\d+)$ index.php?link=6&categoriaid=$2
    
08.11.2016 / 20:31
3

Jonnys, if you do not pass the information on the URL you can not get it to do the treatment in the .htaccess file, as was done in the example you gave.

If you want to have a custom URL without having to leave the explicit ID, consider using slugs, so you search for the slug rather than the ID.

The .htaccess would look something like this for the url www.com.br/categoria-teste:

RewriteEngine On
RewriteRule ^(.*)$ index.php?slug=$1 [L]
    
08.11.2016 / 20:27