killgallic
killgallic

Reputation: 11

Rewriting a URL if it 404's

first timer here so be nice :3.

I am attempting to write a jQuery function that rewrites Amazon URL's to include affiliate tags, similar to what StackExchange does but with a twist.

The main differences is that I am attempting to the user to their closest Amazon Store - e.g. amazon.de - for german visitors. Due to Amazon's ASIN's differing in some countries I first want to check the new link, if it 404's I obviously don't want to direct my visitor there [1]

Here is my code that selects links to amazon.com, grabs the ASIN number and writes a shortlink to the product including the affiliate tag.

var tld_table = {'GB' : ".co.uk",'DE' : ".de",'CN' : ".cn",'AU' : ".ca",'IT' : ".it",'FR' : ".fr",'CA' : ".ca",'JP' : ".jp",};
var country = $.cookie("CountryCode"); 
//$.cookie by http://plugins.jquery.com/files/jquery.cookie.js.txt
var tld = tld_table[country] || '.com';
var regex = RegExp("http://www.amazon.com/([\\w-]+/)?(dp|gp/product)/(\\w+/)?(\\w{10})");
$('a[href*="amazon.com"]').each(function(){
var url = $(this).attr('href');
m = url.match(regex);
    if (m) { //if ASIN found
    var ASIN = m[4];
    var shorturl = "http://www.amazon"+tld+"/dp/" + ASIN + "?tag="+ affTag[tld];
        //http test for 404
        //if 404 do not rewrite
        //else $(this).attr('href',shorturl);
    }
});

This works fine and will re-write the URL's but when I introduce ajax into the equation the script fails to rewrite any URL's.

EDIT

$('a[href*="amazon.com"]').each(function(){
    var url = $(this).attr('href');
    m = url.match(regex);
    if (m) { //if ASIN found http://www.amazon.com/dp/B003DZ1Y8Q/?tag=derp
    var ASIN = m[4];
    var ajaxCall = $.get('ASIN.php?ASIN='+ASIN+'&tld='+tld+'&tag='+affTags[tld], function(data) {
        var newlink = data;
        console.log('New Link: '+newlink)
        $(this).attr('href',newlink); //does not rewrite
    })
    ajaxCall.success(function() {
    if(newlink != '404'){ 
        $(this).attr('href',newlink);//does not rewrite
        }
    })  
    }

});

Above is the code I am attempting to use currently, ASIN.php builds & requests the new link, opens it using php's cURL and returns either a new link or '404'.

I think $(this) is failing to reference the link correctly, but I have no idea why.

Upvotes: 1

Views: 409

Answers (2)

Joe Hanink
Joe Hanink

Reputation: 4847

you can also use apache mod_proxy

ProxyPass /mirror/foo/ http://foo.com/

Then you can call the url /mirror/foo/ on your domain and it will pass the request to the forwarding remote url.

This is a common way of overcoming cross-domain browser restrictions.

http://httpd.apache.org/docs/1.3/mod/mod_proxy.html#proxypass

Upvotes: 0

gnur
gnur

Reputation: 4733

The error says it all: is not allowed by Access-Control-Allow-Origin

It basically means that your javascript is not allowed to retrieve any URL outside of your domain. You can fix this by rewriting your ajax request to a local PHP script that checks the url.

It has something to do with http://en.wikipedia.org/wiki/Same_origin_policy

Upvotes: 2

Related Questions