too long

Our site has a list of games and we get paid everytime someone signs up for them. Since we don't want to show our affiliate link, we have a .htaccess mod_rewrite rule for a url like /play/game-name that ends up going to a php file called redirect.php that pulls that game-name from the url and redirects (via php header location) to corresponding affiliate link for that game. This works, but it seems to be loading really slowly (according to Google Webmasters Tools, anywhere from 10-20 seconds) and it has to be affecting our conversions.

Is there a faster way to do these redirects? It just seems so inefficient to me.

Edit: More info on the process.

Users visit /play/game-name, then .htaccess has the following Rewrite Rule:

RewriteRule ^play/([/_0-9a-zA-Z-]+)$ redirect.php?id=$1 [QSA,L]

Then in redirect.php I have the following to do the redirection:

$path = array(
        'gamename1' => 'http://affiliatelink.com/?a=11111&c=222222&s1=',
        'gamename2' => 'http://affiliatelink.com/?a=11112&c=222223&s1=',
        'gamename3'  => 'http://affiliatelink.com/?a=11113&c=222224&s1=',
        );

    if (array_key_exists($_GET['id'], $path))
    header('Location: ' .$path[$_GET['id']].$s1_value);

The $s1_value is dynamic and pulls the original referrer to the site from a $_SESSION variable.

Build a static mod_rewrite file based on the logic in redirect.php and deploy that to a .htaccess; then the clients won't even touch php.

If you don't know all the variables ahead of time, you can still add them to the .htaccess file as they are generated the first time, essentially the values are generated on the fly, but cached thereafter. The first hit for a given route is slow, PHP builds the rewrite_rule and places it in the .htaccess file. Subsequent requests for said route don't touch the PHP. You'll have to come up w/ a purging method to remove invalid routes if those come up (maybe even dedicate an entire .htaccess file just for these type of routes and delete the entire thing periodically). You'll also need to employ a mutex with this approach.

Here's a boilerplate function you could wrap with a mutex / error handling logic; it also supports the Location redirect as an option.

<?php
/**
 * Generate redirect, either in the form of a Location header
 * (sent directly to STDOUT) or an rewrite rule intended to be
 * placed in a .htaccess file.
 * 
 * @param $sGameName key indicating the game name
 * @param $sReferrer string Additional value appended to result URL
 * @param $bLocationHeader boolean If true send Location header
 *
 * @return string|false If $bLocationHeader is false (default) the
                        mod_rewrite rule is returned.
 *                      Return false if the gamename isn't part of the map
 */
function gen_redirect($sGameName, $sReferrer, $bLocationHeader=false)
{
    // Establish the game name based URL map,
    // this would go in a static variable if gen_redirect was part of a class.
    static $aPaths;
    if($aPaths === null)
        $aPaths = array(
            'gamename1' => 'http://affiliatelink.com/?a=11111&c=222222&s1=',
            'gamename2' => 'http://affiliatelink.com/?a=11112&c=222223&s1=',
            'gamename3' => 'http://affiliatelink.com/?a=11113&c=222224&s1=',
        );

    // Bail if the game name is unknown (might want to do something
    // more substantial here, or as a result of this)
    if(!array_key_exists($sGameName, $aPaths))
        return false;

    $sRedirectUrl = $aPaths[$sGameName] . $sReferrer;

    // Generate the redirect in the form of a Location header
    // and send it to STDOUT
    if($bLocationHeader === true) {
        header('Location: ' . $sRedirectUrl);
        return;
    }

    // Generate the redirect as a mod_rewrite rule
    return 'RewriteRule ^play/' . $sGameName . '$ ' . 
        $sRedirectUrl . ' [QSA,L]';
}

echo gen_redirect('gamename1', 'http://moxune.com') . PHP_EOL;
echo gen_redirect('gamename2', 'http://moxune.com') . PHP_EOL;
echo gen_redirect('gamename3', 'http://moxune.com') . PHP_EOL;

A run of the test script:

bash-3.2$ php htaccess-redirect-builder.php
RewriteRule ^play/gamename1$ http://affiliatelink.com/?a=11111&c=222222&s1=http://moxune.com [QSA,L]
RewriteRule ^play/gamename2$ http://affiliatelink.com/?a=11112&c=222223&s1=http://moxune.com [QSA,L]
RewriteRule ^play/gamename3$ http://affiliatelink.com/?a=11113&c=222224&s1=http://moxune.com [QSA,L]

It looks like you'll need to accept one more inbound parameter to represent the referrer from the clients to pull it off though, essentially your generic rule would be:

/play/[game-name]/[referrer-name] -> http://affiliatelink.com/?[fields-from-url-map]&s1=[referrer-name]

Also, if you're on Linux, I'm a fan of sem_get & friends for a mutex implementation ;)