Ajax software
Free javascripts
↑
Main Page
As you can see in Figure 5-2, the URL doesn’t include any affiliate IDs any more, yet the page retained
the affiliate ID. And you achieved this with only a few lines of PHP code! Everything starts, again, with
a mod_rewrite rule:
RewriteRule ^Products/.*-C([0-9]+)/.*-P([0-9]+)\.html$
i
/product.php?category_id=$1&product_id=$2
&%{QUERY_STRING}
[L]
The
{QUERY_STRING}
bit is a special variable provided by mod_rewrite that represents the query string in
the URL. The
RewriteRule
would still match for keyword-rich URLs that contain query string parameters
(such as
http://seophp.example.com/Products/SEO-Toolbox-C6/Link-Juice-P31.html?aff_
id=987
), but this time you append those parameters to the rewritten URL.
This way, when
product.php
is executed, it’ll have access to the
aff_id
parameter. The only code you’ve
added to
product.php
is minimal, and it consists of verifying if the
aff_id
parameter exists in the query
string. In case it does, you save the affiliate ID value in the user session:
// start PHP session
session_start();
// save affiliate ID
if (isset($_REQUEST[‘aff_id’]))
{
$_SESSION[‘aff_id’] = $_REQUEST[‘aff_id’];
}
The redirection itself is done by our old friend, the
fix_category_product_url()
function. This function
verifies if the URL is identical to a clean product URL, and in case it’s not, it does an automatic 301 redirect
to the “proper” URL; in this case, that proper URL will be a product URL that doesn’t contain the
aff_id
parameter.
// redirect requests proper keyword-rich URLs when not already there
fix_category_product_url();
The first time a product page is loaded with an affiliate ID, it will be redirected to its proper version that
doesn’t contain any query string parameters. When
product.php
reloads, it will reach the code that dis-
plays the product details, and the affiliate ID. This time, the affiliate ID is read from the session:
// display affiliate details
echo ‘<br /><br /> You got here through affiliate: ‘;
if (!isset($_SESSION[‘aff_id’]))
{
echo ‘(no affiliate)‘;
}
else
{
echo $_SESSION[‘aff_id’];
}
Example #2
The second technique involves dynamic URLs. When working with dynamic URLs, the solution is slightly
different. In this case you don’t need to deal with mod_rewrite anymore, but you need a way to read and
remove the affiliate IDs from a dynamic URL, and then redirect to this version of the URL.
111
Chapter 5: Duplicate Content
c05.qxd:c05 10:41 111
Ajax software
Free javascripts
→