hide variable in htaccess url

Asked

Viewed 2,055 times

3

Would you like to know if there is any way to hide a subdirectory or 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 the url that would look like this :

www.com.br/19/categoria-teste 

in this case works ok, but what I really want is to hide the ide from the category to the user and show so

www.com.br/categoria-teste

but of course, passing the id of the variable occult.

2 answers

6


First I recommend you learn , yours is wrong (and is running by "lucky"):

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

Within [...] not using 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

As the ID must be numerical I traded for \d

No way to hide the id that goes in /<id>/categoria-<nome>, see the stackoverflow Urls themselves look like this:

/questions/164248/esconder-variavel-na-url-com-htaccess
/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
  • another way I’ve been searching would be to pass the id via Session while the url would be id-free, so when I clicked the button, I would create asession with the id and on the other page I would receive Session and delete it, so the url is clean , but I couldn’t find a way to create Session with by clicking on a link

  • 1

    @Jonnysj. the idea is good, but it wouldn’t work to index on google.

  • 1

    the legal Uilherme, thank you for informing me, then I will not waste time trying to create it , I will leave same pattern

3

Jonnys, if you do not pass the information in the URL there is no way to 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 the need to make the ID explicit, think about the possibility of using slugs, so you search for Slug and not the ID.

The . htaccess would look something like this for the url www.com.br/test category:

RewriteEngine On
RewriteRule ^(.*)$ index.php?slug=$1 [L]

Browser other questions tagged

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