rewrite版链接重定向再次升级(加密链接)
Posted by 荒野无灯 @ 五月 22, 2010 at 12:09 下午 under wp技巧 10 Comments
前面写了个rewrite版的,发现如果链接中存在?等特殊字符,页面不能跳转到正确的地址。如:
1 | http://wordpress.org.cn/viewthread.php?tid=61140&rpid=502113&fav=yes&ordertype=0&page=1#pid502113 |
这样的地址。
采用加密链接的方法修正此bug 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | ########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\>]+))[^>]*>?(.*?)</a>'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 ############# |
喜欢这篇文章吗?
请订阅本站 RSS feed
相关日志
Comments (10)




哇 这个很强大啊~
这个方法比那个“淫荡”的加框架的要
我用上这个啦,谢谢。gogogo,让我想起澳洲的红绿灯,一到绿灯,交通灯的喇叭就会发出急促的gogogo的声音,很搞笑。
这个可以做成插件了,太牛叉
十分感谢
不过刚刚说的话没有显示啊!
呵呵,有个小bug ,谢谢提醒 ,已经修复 。
谢谢,留言区的链接也加密了,那用邮件回复会有点问题,还有鼠标移至评论列表中的 @someone: 区域不能显示 someone 的评论内容了。不知只在网站链接加密,留言区的链接重定向可不可以!
比如mail to commenter插件 <a href=”#comment-xxx”>@xxx </a>就会用不了呀!
TX指点下啊!可不可以只在网站链接加密,留言区的链接不加密,
试一试再说啊!