How to remove variables from a URL with htaccess

1

When accessing URLs such as the following:

  • https://midominio.net/page/61/?variable_hola=1&saludos=5148B409XXXXX677AB547FB3209AA8C8

  • https://midominio.net/page/20/?variable_hola=1&saludos=5148B409CD818677XX547FB3209AAAC9

  • I want the search parameters to be removed:

    ?variable_hola=1&saludos=5148B409CD8186UUAB547FB3208AA6
    

    I have many URLs of that type, what varies is the code that is at the end. I do not need those parameters, since those variables do not exist, it will not affect that you eliminate them. The same thing appears to the user, whether or not the parameters are present.

    How can I do that?

        
    asked by Erick Estevez 12.03.2018 в 19:09
    source

    2 answers

    2

    If you access page/### (with ### any number of digits) with search parameters, this rule redirects to the same URL but without the parameters:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{QUERY_STRING} .
    RewriteRule ^(page/\d+/?)$ $1 [NC,QSD,R=302,L]
    
    • The flag [QSD] ( query string discard ) just discards the parameters.
    • Whenever you are implementing a redirection, you must first do all the tests with a 302. Only if you are very sure that it works, and that they are not going to change in the future, then you could change it to a 301. Not before, because it is dangerous: 301 redirects are heavily frisked.
    answered by 13.03.2018 / 00:06
    source
    1

    If I understand you correctly, what you want is to eliminate the parameters of the url. You understand therefore that these variables will no longer be accessible. Your .htaccess could be something like this:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/page/([0-9])/(.*)$
    RewriteRule ^(.*) https://midominio.net/page/$1 [R=302,NC]
    
        
    answered by 12.03.2018 в 19:52