Search code examples
.htaccessurl-rewritingfriendly-url

HTACCESS Change URL to be more friendly but still send variable


I want to change this url:

https://www.ugocd.com/ilustracion?rubro=ilustracion

to be rewritten like this:

https://www.ugocd.com/ilustracion

but still send rubro=ilustracion because everything works with this variable in that page and I would like to have a general rule if its possible, because I have other similar pages:

https://www.ugocd.com/arte?rubro=arte

https://www.ugocd.com/animacion?rubro=animacion

https://www.ugocd.com/proyectos?rubro=proyectos

I've tried many things but nothing is working, could anyone please help me.

Thank you

I'tried lots of solutions but nothing worked, I expect to find a solution to my problem.


Solution

  • All you need is a simple rewriting rule:

    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^/?ilustracion$ /ilustracion?rubro=ilustracion [L]
    

    A generic approach would be something like that:

    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^/?(\w+)$ /$1?rubro=$1 [L]
    

    It might be necessary to add a primitive form of a whitelist to prevent other URLs from getting rewritten:

    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{REQUEST_URI} ^(ilustracion|arte|animation|proyectos)$
    RewriteRule ^/?(\w+)$ /$1?rubro=$1 [L]
    

    You may have to adjust those suggestions so that they fit your specific setup. You cannot expect that everything works out of the box when you just blindly copy something without understanding how the tools you are trying to use actually work.