非常感谢原作者分享 这可以说解决了我多年的一个大问题 下面是原文 亲测可行 nginx服务器可以直接用wp的默认伪静态规则 即可 本文用的是 /%category%/%postname%.html 固定连接规则 也推荐这种 如果是其他 需要修改 / 本文收入精华博文
问题
当文章有分页的时候,WordPress默认生成的永久链接是page.html/2的形式,前面一段是文章的$link,后面的数字代表分页$number。那么问题来了,挖掘……不对,从逻辑上讲这到底是个html文件还是一个目录呢?
难看
在.html这个静态文件后面加上一个/和数字简直令人摸不着头脑,这还只是其次,重要的是,我发现搜索引擎根本不收录这样奇怪的链接,这个真是无法接受,我写的东西首尾都很重要,不能因为分个页就被忽视了。
思路
- 利用filter wp_link_pages_link 将分页链接/123456重写为page-[123456].html。
- 利用WordPress或者服务器的RewriteRule将page-[123456].html还原为/123456
- 添加钩子redirect_canonical,防止WordPress从page-[123456].html到/123456的强行跳转。
生成分页html后缀链接
给wordpress主题 的function.PHP 文件加入: (31度批注)
class Rewrite_Inner_Page_Links { var $separaTor; var $post_rule; function __construct() { $this->separator = '/page-'; // (.+?)/([^/]+).html(/[0-9]+)?/? $this->post_rule = '(.+?)/([^/]+)(' . $this->separator . '([0-9]+))+.html/?$'; if (!is_admin() || defined('DOING_AJAX')) : add_filter('wp_link_pages_link', array($this, 'inner_page_link_format'), 10, 2); // for inner pages add_filter('redirect_canonical', array($this, 'cancel_redirect_for_paged_posts'), 10, 2); endif; if (is_admin()) : add_filter('rewrite_rules_array', array($this, 'pagelink_rewrite_rules')); endif; } /** * 修改post分页链接的格式 * @param string $link * @param int $number * @return string */ function inner_page_link_format($link, $number) { if ($number > 1) { if (preg_match('%<a href=".*\.html/\d*"%', $link)) { $link = preg_replace("%(\.html)/(\d*)%", $this->separator . "$2$1", $link); } } return $link; } /** * 为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录 * * 访问原始链接将返回404 * @param array $rules * @return array */ function pagelink_rewrite_rules($rules) { $new_rule[$this->post_rule] = 'index.php?name=$matches[2]&page=$matches[4]'; return $new_rule + $rules; } /** * 禁止WordPress将页面分页链接跳转到原来的格式 * @param string $redirect_url * @param string $requested_url * @return bool */ function cancel_redirect_for_paged_posts($redirect_url, $requested_url) { global $wp_query; if (is_single() && $wp_query->get('page') > 1) { return false; } return true; } } new Rewrite_Inner_Page_Links();
这样就得到了将类似http://www.hankcs.com/program/tokyodaigaku.html/2/ 的分页链接
转化为形如 http://www.hankcs.com/program/tokyodaigaku/page-2.html 的链接。
注意,我的伪静态规则是/%category%/%postname%.html,如果你的规则不同,请自行修改代码或者伪静态规则。
重写URL规则
如果不重写规则的话,WordPress是不认识这个链接的,它以为有个目录叫tokyodaigaku,里面有篇文章叫page-2.html,结果会给出一个无情的404错误:
利用服务器的重写规则
如果是SAE的话,在config.yaml的第一行加入:
- - rewrite: if ( !is_dir() && path ~ "(.+?)/([^/]+)(/page-([0-9]+))+.html/?$") goto "index.php?name=$2&page=$4"
如果不是的话,可以利用WordPress自带的rewrite_rules:
登陆后台——设置——固定链接:
什么也不用填,直接保存更改即可。代码会自动在数据库中硬性加入一条规则:
- "(.+?)/([^/]+)(/page-([0-9]+))+.html/?$" => "index.php?name=$matches[2]&page=$matches[4]"
最终效果
无论是用http://www.hankcs.com/program/tokyodaigaku.html/2/ 还是 http://www.hankcs.com/program/tokyodaigaku/page-2.html,都可以访问第二页。
本站的改造demo https://www.31du.cn/article/seth-speaks-two-1.html 非常成功
原文 https://www.hankcs.com/program/php/wordpress-page-with-the-suffix-html-pseudo-static.html
另外有一篇 WordPress优化之为页面链接添加.html伪静态后缀 https://www.yaxi.net/2016-09-03/1061.html