Without touching rewrites – you could probably do somthing like
website.com/?goto-amazon=<?php echo get_the_ID() ?>
Which’ed output
website.com/?goto-amazon=42
Where 42
is an example of a post that contains the amazon_keywords
.
In your site you could listen for the goto-amazon
query and redirect thereafter
add_action( 'init', function(){
if (isset($_GET['goto-amazon'])) {
$keywds = get_field('amazon_keywords',intval($_GET['goto-amazon']));
$link = "http://www.amazon.co.uk/s/?_encoding=UTF8&camp=1634&creative=19450&field-keywords={$keywds}&linkCode=ur2&tag=AFFID";
wp_redirect($link);
exit;
}
});
UPDATE
To do with rewrites you could so something like so. On your post
$url = get_site_url();
$id = get_the_ID();
echo "<a href="https://wordpress.stackexchange.com/questions/290264/{$url}/goto/amazon/{$id}/">go to Amazon</a>";
Which’d make a link like
website.com/goto/amazon/42/
Then in your functions
add_action( 'init', function(){
// uncomment reload once or twice, or hit SAVE in wp-admin > Settings > Permalinks
//flush_rewrite_rules();
add_rewrite_tag('%gotoamazon%','([^&]+)');
add_rewrite_rule('^goto/amazon/(.*)/?','index.php?gotoamazon=$matches[1]','top');
});
add_action( 'template_redirect', function(){
global $wp_query;
if ($goto = get_query_var( 'gotoamazon' )) {
$keywds = urlencode(get_field('amazon_keywords',intval($goto)));
$link = "http://www.amazon.co.uk/s/?_encoding=UTF8&camp=1634&creative=19450&field-keywords={$keywds}&linkCode=ur2&tag=AFFID";
wp_redirect($link);
exit;
}
});
When dealing with rewrites, make sure you reset the rewrite rules, instructions in commented code above.
I don’t know how amazon expects the keywords, but they should be urlencode()
‘ed