1

My .htaccess file changes (for example) example.com/brands/beams/... to example.com/php/mvc.php?htTargUrl=brands/beams/... with QSA rule.

Here's the rule:

RewriteRule ^(.+)$ php/mvc.php?htTargUrl=$1 [QSA,L,B,BNP,NC] 

And I need to remove htTargUrl parameter from original URL. For example: example.com/brands/beams/?htTargUrl=beams&par2=val to example.com/brands/beams/?par2=val.

I've tried many rules to do this, but none worked with the query string. If I use

RewriteCond %{QUERY_STRING} ^(.*)&?htTargUrl=[^&]+&?(.*)$ [NC] RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L] 

then the parameter removes not only from the original link and URL becomes example.com/php/mvc.php.

How can I do this?

1 Answer 1

3
RewriteCond %{QUERY_STRING} ^(.*)&?htTargUrl=[^&]+&?(.*)$ [NC] RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L] 

By itself, the rule you posted should already do what you require and remove the htTargUrl=<something> URL parameter from the requested URL.

However, you need to avoid removing the URL parameter from the rewritten URL - which is what appears to be happening here. You can do this with an additional condition to target direct requests only, not rewritten requests.

For example:

RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{QUERY_STRING} ^(.*)&?htTargUrl=[^&]+&?(.*)$ [NC] RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L] 

This rule also needs to go near the top of your .htaccess file, before an existing rewrites.

By checking against the REDIRECT_STATUS environment variable we ensure that only direct requests are redirected. REDIRECT_STATUS is empty on the initial request and set to "200" (as in 200 OK HTTP response code) after the first successful rewrite.

You will need to make sure that your browser cache is cleared, since any erroneous 301 (permanent) redirects will have been cached by the browser. (For this reason it is advisable to first test with 302 - temporary - redirects.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.