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
Webmasters Stack Exchange is a question and answer site for webmasters. It only takes a minute to sign up.
Sign up to join this communityAssuming 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.
.htaccess
only or PHP? What is the reason for this redirect? Have URLs changed? Is this permanent?