解决 wordpress 使用 cos-html-cache 插件后评论分页和画廊的问题

事情是这样的,从7月份起,谷奥的wordpress全面采用了东哥cos-html-cache插件对首页以及帖子进行静态化处理,效果非常不错。但是使用一段时间后发现了几个和原始程序冲突的小地方,想了个比较笨的方法解决了一下,本人编程小白,憋了半天鼓捣出的代码,各位读者见笑了。如果您有更好的解决方法,欢迎和大家分享。

1,评论分页问题:

谷奥的permalink格式是

http://google.org.cn/posts/google-wave-nominate-for-guao-readers.html/

这样,这里的.html是一个真正的静态化htlm文件,如果在后台打开了评论分页的话,分页链接就会是类似

http://google.org.cn/posts/google-wave-nominate-for-guao-readers.html/comment-page-12#comments

这样的格式。很显然这个链接会返回404

2,Feed中的画廊跳转问题:

这里的画廊是指wordpress自带的gallery,比如这个帖子。如果在gallery的属性中,把"Link thumbnails to:"选成Image File后,直接在浏览器打开帖子时gallery图片的链接是没有问题的(直接链接到图片本身)。但是在rss中,图片的链接却是

http://android.google.org.cn/posts/samsung-behold-ii-caught-behind-glass.html/samsung-behold-ii-ctia-05

这样的格式,显然又是一个404。

3,解决方法

经过考虑后,觉得还是使用301来对这种url进行处理最为简便:分页评论直接指向index.php处理的预览结果(使用预览(?preview=true)可以防止跳转到permalink,感谢东哥的这个tip),而图片链接则直接跳转到图片本身。具体方法如下,修改.htaccess:

#redirect paged comments
RedirectMatch 301 /posts/(.*).html/comment-page-(.*)  http://google.org.cn/gkp/guao-paged-comment-tran.php?cpage=$2\&permalink=$1

#redirect gallery in feeds
RedirectMatch 301 /posts/(.*).html/(.*) http://google.org.cn/gkp/guao-gallery-tran.php?picname=$2

直接将分页和rss图片跳转的url 301到php文件,guao-paged-comment-tran.php:

<?
include(‘./config.php’);
$permalink = $_GET[‘permalink’];
$permalink = formpost($permalink);
$cpage = $_GET[‘cpage’];
$cpage = formpost($cpage);
$sql = "SELECT ID FROM wp_posts WHERE post_name = ‘$permalink’";
$query = $gkp->query($sql);
$post_id = mysql_result($query,0);

Header(  "HTTP/1.1  301  Moved  Permanently"  );
Header(  "Location:  http://google.org.cn/index.php?preview=true&p=$post_id&cpage=$cpage"  );
?>

原理很简单,从原始url取到permalink与页号,通过数据库查询得出ID,并再次301到预览格式,经此处理后,上面例子中的:

http://google.org.cn/posts/google-wave-nominate-for-guao-readers.html/comment-page-12#comments

就可以跳转到:

http://google.org.cn/posts/google-wave-nominate-for-guao-readers.html/comment-page-12#comments

解决了分页评论的问题。

下面是图片跳转,guao-gallery-tran.php,原理类似,可以将rss画廊的图片链接直接跳转到图片的url。

<?
include(‘./config.php’);
$picname = $_GET[‘picname’];
$picname = formpost($picname);
$sql = "SELECT guid FROM wp_posts WHERE post_name = ‘$picname’";
$query = $gkp->query($sql);
$picurl = mysql_result($query,0);

Header(  "HTTP/1.1  301  Moved  Permanently"  );
Header(  "Location:  $picurl"  );
?>

This entry was posted in 电脑相关 and tagged , , . Bookmark the permalink.

3 Responses to 解决 wordpress 使用 cos-html-cache 插件后评论分页和画廊的问题

  1. cosbeta says:

    不容易啊,不容易!

  2. gkp says:

    东哥什么时候升级,加入更多的页面的静态化呀……

  3. 我测试下 says:

    我测试一下,呵呵

Leave a Reply

Your email address will not be published.