WordPress 提供了多种结构标签,以便我们可以设置各种格式的永久链接结构,再配合一些静态化插件(例如 cos-html-cache),就可以使页面真正静态化。
不过 WordPress 对已分页文章的永久链接的处理方式则会给页面静态化后的访问带来问题。 例如,永久链接结构为 /%year%/%monthnum%/%postname%.html,WordPress 生成的文章相关分页链接如下所示:
- yourdomain.com/2008/03/postname.html
- yourdomain.com/2008/03/postname.html/2
- yourdomain.com/2008/03/postname.html/3
可以看到 WordPress 只是简单地将页码加在了链接尾部,所以当我们静态化其中一页的内容后,我们将只能访问被静态化的那一页内容而无法访问其它分页的内容。为了可以静态化所有分页内容,需要对 WordPress 处理永久链接的方式做些小小的改动,并改变分页链接的形式:
- yourdomain.com/2008/03/postname.html
- yourdomain.com/2008/03/postname_2.html
- yourdomain.com/2008/03/postname_3.html
于是利用 WordPress 本身提供的接口实现了更好的解决方案。
以/%year%/%monthnum%/%postname%.html这样的永久链接结构为例:
1. 打开主题目录下的functions.php文件,添加以下代码:
- // 添加分页处理规则
- function add_custom_post_rewrite_rules($rules) {
- $custom_rules = array(
- '([0-9]{4})/([0-9]{1,2})/([^/]+)-([0-9]+)\.html$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&name=$matches[3]&page=$matches[4]',
- );
- $rules = array_merge($custom_rules, $rules);
- return $rules;
- }
- add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');
- // 修改分页链接
- function my_wp_link_pages($args = '') {
- $args .= ($args ? '&' : '') . 'echo=0';
- $links = wp_link_pages($args);
- $links = preg_replace_callback('|([0-9]{4}/[0-9]{1,2}/)([^/]+)(\.html)(/)([0-9]+)|', 'custom_page_link', $links);
- echo $links;
- }
- function custom_page_link($matches) {
- return $matches[1].$matches[2].'-'.$matches[5].$matches[3];
- }
2. 打开主题目录下的single.php文件,查找wp_link_pages并替换为my_wp_link_pages。
3. 后台“设置-永久链接”点击一下“保存修改”按钮,大功告成。具体效果可以看本文
下面是几种其它永久链接结构的具体修改代码:
- 固定链接格式为:/%year%/%monthnum%%day%/%postname%.html
- // 添加分页处理规则
- function add_custom_post_rewrite_rules($rules) {
- $custom_rules = array(
- '([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)-([0-9]+)\.html$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5]',
- );
- $rules = array_merge($custom_rules, $rules);
- return $rules;
- }
- add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');
- // 修改分页链接
- function my_wp_link_pages($args = '') {
- $args .= ($args ? '&' : '') . 'echo=0';
- $links = wp_link_pages($args);
- $links = preg_replace_callback('|([0-9]{4}/[0-9]{1,2}[0-9]{1,2}/)([^/]+)(\.html)(/)([0-9]+)|', 'custom_page_link', $links);
- echo $links;
- }
- function custom_page_link($matches) {
- return $matches[1].$matches[2].'-'.$matches[5].$matches[3];
- }
相关Apache下的RewriteRule
- <IfModule mod_rewrite.c>
- RewriteEngine On
- RewriteBase /
- RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/trackback/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&tb=1 [L]
- RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/feed/(feed|rdf|rss|rss2|atom)/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&feed=$5 [L]
- RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/(feed|rdf|rss|rss2|atom)/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&feed=$5 [L]
- RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/page/?([0-9]{1,})/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&paged=$5
- RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/([0-9]+)/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&page=$5
- </IfModule>
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog
我的robots都是直接隐蔽/data/,也不知道有没有必要~~
正是需要的,谢谢博主分享。
页面下方的页面链接显示“http://www.m5050.com/freeziyuan/453-2.html”
但打开时显示http://www.m5050.com/freeziyuan/453.html/2
这是为什么啊? 😥
固定链接格式设置对吗?检查下后台固定链接的格式
我的也是 求解释
查看一下你后台固定链接格式设置的对吗?这个是要改的 😛
【状况同上】固定链接设置对了
""
""
"
po主,我按照上面的设置了,一步步设置,一步没差;
href链接显示正常,一点击(或者手动输入)就跳转了
http://127.0.0.1/html/2013/05/19/xxx-3.html
跳为:
http://127.0.0.1/html/2013/05/19/xxx.html/3
希望po主指教下 😥
"
2. 打开主题目录下的single.php文件,查找wp_link_pages并替换为my_wp_link_pages这步确定没有少吗,鼠标放到分页上看下链接对不对 😳
鼠标放在链接上显示的是对的,xxx-3.html格式,就是一点击就跳了,
诡异的是当我把后台的固定链接给为/p?=123的格式,虽然链接不显示静态格式,但是手动输入却能正常按照xxx-3.html的格式访问。
还真是诡异啊,你用的什么web服务器,apahce还nginx,你是不是配置了伪静态了
apahce,配置了伪静态,请问您的wordpress 什么版本?我的是最新版本 ❓
我apache和nginx都用过,都可以用啊,不用配置伪静态规则,我的.htaccess里也没什么
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
用nginx时也就配置了
location /
{
if (!-e $request_filename)
{
rewrite (.*) /index.php last;
}
}
你好,如果是nginx的伪静态应该如何弄呢?
不用做设置就可以 😛
if (!-e $request_filename)
{
rewrite (.*) /index.php last;
}
我用的是lnmp上默认的,也不用修改吗?
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
是的,不用修改就可以的
🙂 非常感谢
我也遇到这样的问题
http://www.geelg.com/creative/12328_3.html 点击之后就显示了下面的链入
http://www.geelg.com/creative/12328.html/3