douzuanze0486 2012-02-03 21:57
浏览 10
已采纳

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.

  • 写回答

1条回答 默认 最新

  • doutou7549 2012-02-03 22:41
    关注

    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 ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection