0

How to change:

domain.com/go/?custom_ref=001

into

domain.com/go/?ref=001

using htaccess rewrite please?

1 Answers1

0

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.

Community
  • 1
  • 1
Stecman
  • 2,890
  • 20
  • 18
  • Thank you. It rewrites link on the page but still appears as ?custom_ref= in the browser url. Is there any way to change that to ?ref= – Metalo Polis Nov 13 '12 at 20:34