Search code examples
.htaccess

How to prevent .htaccess rule adding endlessly repeated parameter at the end of the URL


I'm building a website (in Joomla 4, if that's relevant), and I'm trying to use .htaccess to redirect URLs like this:

index.php/characters

... to URLs like this:

index.php/characters?reset=true

... but leave URLs like this alone:

index.php/characters?search=fred

I used https://htaccess.madewithlove.com/ to test the following rules, and the simulation looked like it would achieve exactly what I want:

  RewriteCond %{QUERY_STRING} ^(?!search)
  RewriteRule index.php/characters /index.php/characters?reset=true [L,R,QSA]

But this gives me endless redirects which look like this:

index.php/characters?reset=true&reset=true&reset=true&reset=true&reset=true&reset=true&reset=true&reset=true (etc)

What am I doing wrong here?


Solution

  • You may use a redirect rule like this:

    RewriteCond %{QUERY_STRING} !(^|&)(search|reset)= [NC]
    RewriteRule ^index\.php/characters/?$ /$0?reset=true [L,R,QSA]
    

    Negative condition RewriteCond %{QUERY_STRING} !(^|&)(search|reset)= will skip the rule if query string has a parameter that starts with search= or reset=.