How in the .htaccess file to make a redirect from / browse to / browse? Id = 2?

The code Redirect 301 /browse https://site.net/browse?id=2 creates a cyclic redirect.

    1 answer 1

     RewriteEngine On # проверяем отсутствие переменной id со значением 2 в запросе # "\b" в начале и конце нужны чтобы проверка не сработала на, например, pid=2 или id=22 RewriteCond %{QUERY_STRING} !\bid=2\b # сама переадресация с /browse на /browse?id=2 без передачи дополнительных переменных из оригинального запроса RewriteRule ^browse$ /browse?id=2? [R=301,L] 

    Another variant

     RewriteEngine On # проверяем отсутствие переменных в запросе RewriteCond %{QUERY_STRING} ^$ # сама переадресация с /browse на /browse?id=2 RewriteRule ^browse$ /browse?id=2 [R=301,L] 

    Last option :)

     RewriteEngine On # проверяем отсутствие переменной id в запросе RewriteCond %{QUERY_STRING} !\bid= # сама переадресация с /browse на /browse?id=2 RewriteRule ^browse$ /browse?id=2 [R=301,L] 
    • Redirection works, however pages / browse? Id = 4 (or any other parameter instead of 4) are also redirected to / browse? Id = 2, besides adding% 3f to the end of the link. - / browse? id = 2% 3f - faik371
    • @ faik371, added another option with redirection only if there are no any GET variables in the request. - Visman
    • The second option does not work at all. - faik371
    • @ faik371, maybe your QUERY_STRING not empty, so it doesn’t work. I proposed the last option with checking for the absence of the id variable only. - Visman
    • Thanks, this option has earned! - faik371