rewrite版链接重定向再次升级(加密链接) » 荒野无灯weblog

Keep It Simple, Stupid.

荒野无灯weblog

rewrite版链接重定向再次升级(加密链接)

前面写了个rewrite版的,发现如果链接中存在?等特殊字符,页面不能跳转到正确的地址。如:


http://wordpress.org.cn/viewthread.php?tid=61140&rpid=502113&fav=yes&ordertype=0&page=1#pid502113

这样的地址。
采用加密链接的方法修正此bug 。 :mrgreen:

########START########### 链接重定向处理 by 荒野无灯  http://www.ihacklog.com    ############START#############

function match_links($content) 
{
$match=array();
preg_match_all("'<\s*a\s.*?href\s*=\s*([\"\'])?(?(1)(.*?)\\1|([^\s\>]+))[^>]*>?(.*?)'isx",$content,$links);
while(list($key,$val) = each($links[2])) {
if(!empty($val)   &&  !preg_match("@\.(jpg|gif|png|rar|zip|gz|tgz|swf|js|txt)$@i",$val)  )
$match['link'][] = $val;
}
while(list($key,$val) = each($links[3])) {
if(!empty($val))
$match['link'][] = $val;
}
while(list($key,$val) = each($links[4])) {
if(!empty($val))
$match['content'][] = $val;
}
while(list($key,$val) = each($links[0])) {
if(!empty($val))
$match['all'][] = $val;
}
return $match;
}

function add_comment_link_redirect($content='')
{
    $l=match_links($content);
    if(!isset($l['link']))
        return $content;
    $cnt=count($l['link']);
    for($i=0;$i<$cnt;$i++)
    {
        $rep[]=get_option('home').'/gogogo/'.base64_encode( str_replace('&','&',$l['link'][$i]) );
    }
    return str_replace($l['link'],$rep,$content);
}

add_filter('get_comment_author_link', 'add_comment_link_redirect', 5);
add_filter('comment_text', 'add_comment_link_redirect', 99);

add_filter('query_vars', 'hacklog_comment_redirect_go_query_vars');
function hacklog_comment_redirect_go_query_vars($public_query_vars) {
    $public_query_vars[] = "hacklog_go_url";
    return $public_query_vars;
}

add_filter('generate_rewrite_rules', 'hacklog_comment_redirect_rewrite');
function hacklog_comment_redirect_rewrite($wp_rewrite)
{
    $wp_rewrite->rules = array_merge(array('gogogo/(.*)$' => 'index.php?hacklog_go_url=$matches[1]'), $wp_rewrite->rules);
}

add_action('template_redirect', 'hacklog_comment_redirect_go',6);
function hacklog_comment_redirect_go()
{
$url = get_query_var('hacklog_go_url');
$errorPage='http://www.ihacklog.com/error.html';
//$home='http://www.ihacklog.com';
$home=$_SERVER['HTTP_HOST'];
$refer= empty($_SERVER['HTTP_REFERER'])?'http://www.ihacklog.com':$_SERVER['HTTP_REFERER'];

if(!empty($url)) 
{
        if(false !==strpos($url,'aHR0cDovL') )
            $url=base64_decode($url);
        $url=(!preg_match("/^http\:\/\//i", $url))? "http://".$url:$url;

       if(strpos($refer,$home)===false)  //非本站引用
       {
        header('Location:'.$errorPage);
        exit();
       }

        header('Location:'.$url);//直接跳转
         exit();
} 
}

######## END ########### 链接重定向处理 by 荒野无灯  http://www.ihacklog.com    ############ END #############

Tagged in : PHP,wordpress,链接重定向,redirect,rewrite

All Comments (0)
Gravatar image
No Comments