Home > Enterprise >  .htaccess rewrite rule for affiliate links
.htaccess rewrite rule for affiliate links

Time:11-05

I've many links of this type:

https://example.com/?redirectTo=G04BIQ8LGG&redirect_prodid=xyz-G04BIQ8LGG

I need to redirect to amazon affiliate link:

https://www.amazon.com/dp/G04BIQ8LGG?tag=mytag-21&linkCode=ogi&th=1&psc=1

The only part to take from old url is the product code (ex.G04BIQ8LGG)

Someone can help me with .htaccess rule and regex?

Thanks!

CodePudding user response:

unfortunately no, I'm not very good with regex.

The regex is very similar as in the linked question. However, the required mod_rewrite directives themselves are much simpler in this case.

For example:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^redirectTo=([^&] )
RewriteRule ^$ https://www.amazon.com/dp/%1?tag=mytag-21&linkCode=ogi&th=1&psc=1 [NE,R=302,L]

I've anchored the redirectTo URL parameter to the start of the query string, since that is how it appears in your example. In the linked question, the URL parameter can appear anywhere in the query string since that would seem to have been a requirement in that question.

Since the URL parameter value is used in the URL-path of the redirected URL, the NE (noescape) flag is required to prevent a %-encoded URL param value being doubly encoded in the resulting redirect. (Although this is not an issue if this URL param value is never %-encoded - it doesn't necessarily look as if it would be.)

  • Related