I'm trying to write some RewriteRules so that when a request contains a specific query parameter, let's call it foo, it is ignored (removed) completely. The reason for this is that I need to use foo internally in another RewriteRule so that I can pass information to my front controller. If foo would already exist, the application wouldn't work as intended. I tried multiple solutions, for example this one, but that doesn't do what I'd like to achieve. So far, I tried the following:
<IfModule mod_rewrite.c>
RewriteEngine On
# Remove possible existing query parameter called 'foo'
RewriteCond %{QUERY_STRING} ^(.*)&?foo=[^&]+&?(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]
# Lead every page/API request to index.php
RewriteCond $1 !\.(svg|jpg|png|webp|ico|css|js|ttf|woff|woff2)$
RewriteRule ^(.*)$ index.php?foo=$1 [L,QSA]
</IfModule>
Basically, every request which doesn't end with one of the listed file extensions should be passed to my front controller. Without the lines 5 and 6 everything works fine as long as no foo query parameter exists in the original request.
Is there a way to just remove the foo parameter without affecting the other RewriteRule (or simply to override it in the second part)? Thanks in advance!
Edit
Just to clarify a few things: In my application, the front controller is a PHP script that takes the rewritten GET parameter foo, which contains the originally requested URL. However, if the user adds foo to the request, I have to make sure this is not the foo parameter that gets passed to the front controller as the app wouldn't work properly then.
For example: If the client requests https://example.com/something?foo=123&bar=456, the request should be redirected to https://example.com/something?bar=456 (or alternatively not redirected at all) and the query parameter foo in line 9, which is intended for my front controller only, should be https://example.com/something with bar not being changed. Unfortunately, the above code wont't do this. Instead, I'm redirected to https://example.com/index.php?bar=456. I guess the solution is actually pretty simple, but I'm not able to find out how to do it.