2

For WHMCS I am trying to redirect products to another ID and subdomain.

How can I create a .htaccess that will redirect

https://www.example.com/clients/cart.php?a=add&pid=17

to

https://clients.example.com/cart.php?a=add&pid=55

1 Answer 1

1

Assuming the query string is always exactly as written. ie. just two URL parameters and in the order: a first, followed by pid. Then you can do something like the following using mod_rewrite in the root .htaccess file:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^a=add&pid=17$
RewriteRule ^(clients)/(cart\.php)$ https://$1.example.com/$2?a=add&pid=55 [R=302,L]

The $1 and $2 backreferences simply save repetition as they contain the captured subgroups from the preceding RewriteRule pattern.

If, however, the URL parameters could be in any order and there might be other URL params (that should be discarded) then you would need to modify the conditions (RewriteCond directives) like so:

RewriteCond %{QUERY_STRING} (^|&)a=add(&|$)
RewriteCond %{QUERY_STRING} (^|&)pid=17(&|$)
:

If you have many such redirects then they can be simplified, either by using a RewriteMap (defined in the server config) or perhaps a secondary .htaccess file in a specific subdirectory, if you do not have access to the server config. Or root such requests to a server-side PHP script that handles the redirect.

2
  • What if I want to modify multiple for example pid=18 then to pid=59
    – Toodarday
    Jan 24, 2023 at 9:48
  • @Toodarday How many? If it's just a few (dozen or so) then just repeat the above rule as many times as required. Otherwise, what access to the server do you have? Server config, .htaccess only or PHP? What is the reason for this redirect? Have URLs changed? Is this permanent?
    – MrWhite
    Jan 24, 2023 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.