alex-schuster
alex-schuster

Reputation: 142

Combine RewriteRules in .htaccess so that a specific query parameter is ignored

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.

Upvotes: 3

Views: 460

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133770

Could you please try following. please make sure you clear your browser cache before testing your URLs. Considering here you need everything after & in query string to be passed in rewritten URL here. Added 2 existing rules from OP's htaccess file and changed place for https rule moved it to very first place.

RewriteEngine ON
##From OP's comments adding these here, http --> https should come first.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

# Remove possible existing query parameter called 'foo'
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*)&?foo=[^&]+&?(.*)$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}?%1%2 [NE,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]

Upvotes: 4

Related Questions