How to redirect and pass URL parameters with htacccess?

1.1k Views Asked by At

How to redirect affiliate link and add tracking code?

I have an WordPress website. There is an affiliate link on home page. I redirect (cloak) this link with .htaccess Redirect:

Redirect 302 /go/besthost https://www.affiliatepanel.com/?id=12345

My website is on example.com.

Running ads I send URL Parameters

https://www.example.com?ad=adname&camp=cname&source=google

How to read URL parameters and add them to affiliate link so it should become

https://www.affiliatepanel.com/?id=12345&ad=adname&camp=cname&source=google
1

There are 1 best solutions below

0
On

In order to pass the URL parameters you'll need to use a mod_rewrite RewriteRule (redirect), rather than a mod_alias Redirect directive.

For example, if the request is https://www.example.com/url-path?url-parameters and you want to redirect this to https://www.affiliatepanel.com/?id=12345&url-parameters then you would do something like the following using mod_rewrite at the top of your .htaccess file:

RewriteEngine On

RewriteRule ^url-path$ https://www.affiliatepanel.com/?id=12345 [QSA,R=302,L]

The QSA flag (Query string Append) does the "magic" of appending the original query string (ie. "url-parameters") from the request on to the redirected URL.

If it wasn't for the fact you are adding a new query string (ie. id=12345) then you wouldn't have to do anything, since the query string is appended by default (as long as you don't add a new one).