How to change:
domain.com/go/?custom_ref=001
into
domain.com/go/?ref=001
using htaccess rewrite please?
How to change:
domain.com/go/?custom_ref=001
into
domain.com/go/?ref=001
using htaccess rewrite please?
Not particularly flexible, but this works for your exact problem:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ref=([0-9]*)
RewriteRule ^go/$ go/?custom_ref=%1 [QSA]
Note that for the condition to match, ref
must come first in the query string. The [QSA]
(Query String Append) flag on the rule will retain the original query string, so other values passed will not be lost.
Also note that this code assumes the .htaccess file is in the document root. If the file is elsewhere, you will need to adjust the RewriteRule
. For example, changing it to the following will work when the .htaccess file is placed at /go/.htaccess:
RewriteRule . ?custom_ref=%1 [QSA]
If you want this to be more flexible, check out the responses to this question - they contain the syntax for changing a single variable in the query string and retaining everything else.
Edit:
To replace custom_ref
with ref
in the query string, you could use the following to redirect requests containing the custom_ref
variable. Add this between RewriteEngine On
and the other rules:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)custom_ref=(.*)
RewriteRule ^go/$ /go/?%1ref=%3 [R=301,L]
This is more of a patch than a solution. If you have access to the code, I recommend fixing the variable names there instead.