Redirect a URL in .htaccess, but keep the query string

I want to redirect a URL in .htaccess, but want to keep the (dynamic) parameters from the query string at the end of the URL (e.g. ?id=1660, ?id=1661, etc.)

E.g. https://mywebsite.example/service/viewinvoice.php?id=1660

I want to redirect it to:

https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=1660

So basically: https://mywebsite.example/service/viewinvoice.php?id=... needs to be redirected to https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=...

I tried this below, without any success

 RewriteEngine On

RewriteCond %{QUERY_STRING} (^|&)/service/viewinvoice.php?id= [NC]
RewriteRule ^/?$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301]

I think this is not the right solution. Does someone has suggestions?

Upvotes: 0

Views: 1434

Answers (1)

Stephen Ostermiller
Stephen Ostermiller

Reputation: 25535

You need to use the QSA (Query String Append) flag on your rewrite rule. From the documentation:

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

From your example URLs, you don't need to match the query string in a rewrite condition. You are matching the URL path which is done as the first part of the rewrite rule itself.

Your rule should be:

RewriteEngine On
RewriteRule ^/?service/viewinvoice\.php$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301,QSA]

Upvotes: 0

Related Questions