Lazy Amz Affs: Quick masked 301'd product searches

Nat

Joined
Nov 18, 2014
Messages
555
Likes
345
Degree
2
Thought this might be useful to someone after I realized it would save me some time on a few pages... Pretty short code so I'm just going to paste it in here. I might have made some mistakes, so I'd love some criticism. This grabs a product name, identification number, or any random text you want and then searches for it on Amazon. However, instead of linking out to Amazon it masks it on your domain. I also added a Wordpress short code.

#1 - Create a folder public_html/go-amazon and add .htaccess and index.php

#2 - .htaccess

Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ([^/]*)/?(.*)$ index.php?id=$1&second=$2
</IfModule>

You can also add some code above </IfModule> to block Googlebot. Something like:
Code:
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|Baiduspider) [NC]
RewriteRule .* - [R=403,L]

#3 - index.php

Replace the XXXXXXXXXXX-20 with your tracking ID. $product_index can be removed or set to only search a specific category. Or, you can change a few things and have the second url parameter set product_index.

Code:
<?php
$amazon_aff_id = 'XXXXXXXXXXX-20';
$redirect_to = 'https://www.amazon.com/s/?url=search-alias&field-keywords=';
$product_index = 'kitchen';

$id = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'error';
$second = isset( $_GET['second'] ) ? rtrim( trim( $_GET['second'] ), '/' ) : '';

if (!($second))
{
$final_redirect_to = $redirect_to . urlencode($id) . '&tag=' . $amazon_aff_id . '&linkCode=qs&index=' . $product_index . '&ie=UTF8';
}
else{
//here you can use the value of $second to change the search structure in some way.... you could make a second $redirect_to, set $product_index, change your id, etc.
}
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: $final_redirect_to" );

#4 - Modify your robots.txt

Code:
User-agent: *
Disallow: /go-amazon/

How it works
Now you can pass words / product names into your domain on the fly.

example.com/go-amazon/kindle --> Redirects to the Amazon search page for the kindle.
example.com/go-amazon/green+powder --> Redirects to the Amazon search page for green powder.
example.com/go-amazon/green powder --> Should also redirect to the Amazon search page for green powder.

Depending on your needs, you can add another '/' and utilize the second parameter.

example.com/go-amazon/green+powder/kitchen --> Easily configured to search Amazon's kitchen dept. for green poweder.

Or you can just check for the existence of /anything and then switch up the url structure. If you're willing to take the time to find the product ID you can go directly to a product page.... but a search is very useful for products that have multiple sellers or they go in and out of stock frequently.

Its also easy to switch out some things and use another affiliate site that allows for searches. Or to generate links to both. And so on.

#5 - (Optional) Wordpress shortcode - Find your theme or child theme's functions.php
The fields in $a are totally optional -- you can add more/remove, but I left the basic structure for people unfamiliar with code. If you want to do some scraping you'll find ways to populate more. (You can also modify the amazon link to use a second parameter.)
Code:
function amazon_search( $atts, $content = null ) {
    $a = shortcode_atts( array(
        'name' => '',
        'quickinfo' =>'',
        'class' => 'style-how-you-like',
        'style' =>  ''
    ), $atts );
if (!empty($a['name'])) {
$name = $a['name'];
}
else{
$name = ($content);
}
$amazon_url = 'https://YOURDOMAIN.com/search-amazon/' . $name;
$return_info = '<div class="' . $a['class'] . '"><span class="name">' . $name . '</span><br />';
$return_info .= '<a href="' . $amazon_url .'" target="_blank" rel="nofollow"><span class="style-how-you-like">Search on Amazon</span></a> ';

return $return_info;
}
add_shortcode( 'amazon_search', 'amazon_search' );

[amazon_search]Kindle[/amazon_search] should return a stylized "Kindle" with a hyperlinked Search on Amazon link below it --> YOURDOMAIN.com/go-amazon/kindle

An even more stripped down version

Code:
function amazon_search( $atts, $content = null ) {
$amazon_url = 'https://YOURDOMAIN.com/search-amazon/' . $content;
$return_info .= '<a href="' . $amazon_url .'" target="_blank" rel="nofollow"><span class="style-how-you-like">' . $content . '</span></a> ';
return $return_info;
}
add_shortcode( 'amazon_search', 'amazon_search' );

[amazon_search]Kindle[/amazon_search] should return "Kindle" hyperlinked to YOURDOMAIN.com/go-amazon/kindle. Oh, and I simplified all the code in this composition box, so its at risk of needing an additional ' or ; somewhere.

Hope this helps someone, or I learn something by get called out for doing something wrong.
 
Just remembered that most people use a 302 redirect for affiliate links. To change it, use
Code:
header("Location: $final_redirect_to");
Instead of the two lines above.

Another thing, (I have a hunch this might be a bad idea with Amazon), is to host this all on a separate domain. So, AuthoritySite.com uses shortniche.to/go-to-amazon/8hef8. Which is easy to do and change if all of your links are generated from a shortcode.
 
Back