wordpress修改固定链接后301重定向的方法

所属分类: CMS教程 / WordPress 阅读数: 1159
收藏 0 赞 0 分享

以前小站的固定链接好不人性化,结构是/%year%/%monthnum%/%postname%/,这样看起来又不直观又长的,今天做了一个301重定向,然后修改了新的固定链接,现在的固定链接格式是/%postname%.html,这样看起来短了好多,也直观了好多,
不过小站已经被搜索引擎收录了好多,以前的地址都无法访问了,所以要做好301重定向,跳转到新的固定链接所生成的地址中去.要不然都是404就悲剧了.下面贴出解决办法


复制代码
代码如下:

$rewrite_config = array();
$rewrite_config['highpriority'] = true ;
$rewrite_config['rewrite'] = array();
$rewrite_config['oldstructure'] = "/%year%/%monthnum%/%postname%/";

function wpdaxue_pm_the_posts($post) {
global $wp;
global $wp_rewrite;
global $rewrite_config;

$rewrite_config['rewrite'] = $wp_rewrite->generate_rewrite_rule($rewrite_config['oldstructure'], false, true, true, true);
if ($post != NULL && is_single() && $rewrite_config['oldstructure'] != $wp_rewrite->permalink_structure) {
if (array_key_exists($wp->matched_rule, $rewrite_config['rewrite'])) {
// ok, we need to generate a 301 Permanent redirect here.
header("HTTP/1.1 301 Moved Permanently", TRUE, 301);
header('Status: 301 Moved Permanently');
$permalink = get_permalink($post[0]->ID);
if (is_feed()) {
$permalink = trailingslashit($permalink) . 'feed/';
}
header("Location: ". $permalink);
exit();
}
}
return $post;
}

function wpdaxue_pm_post_rewrite_rules($rules) {
global $wp_rewrite;
global $rewrite_config;
$oldstruct = $rewrite_config['oldstructure'];

if ($oldstruct != NULL && $oldstruct != $wp_rewrite->permalink_structure) {
$rewrite_config['rewrite'] = $wp_rewrite->generate_rewrite_rule($oldstruct, false, true, true, true);
if ($rewrite_config ['highpriority'] == true) {
return array_merge($rewrite_config['rewrite'], $rules);
} else {
return array_merge($rules, $rewrite_config['rewrite']);
}
}
return $rules;
}
add_filter('the_posts', 'wpdaxue_pm_the_posts', 20);
add_filter('post_rewrite_rules', 'wpdaxue_pm_post_rewrite_rules');

将我的$rewrite_config['oldstructure'] = “/%year%/%monthnum%/%postname%/”后面的/%year%/%monthnum%/%postname%/修改成你自己的旧的固定链接格式,然后将这段代码加入到主题的function.php中,然后设置成新的固定链接格式就搞定了.

更多精彩内容其他人还在看

wordpress 为主题添加AJAX提交评论功能的php代码

前几天,为了提高评论体验,为了减轻服务器负担,我为主题添加了AJAX提交评论功能,现在分享一下方法。
收藏 0 赞 0 分享

wordpress 优化指南

wordpress毫无疑问是一个优秀的博客系统,其最吸引人的一个特点就是有大量的外观主题(theme)可以满足个性化的博客展示需求。
收藏 0 赞 0 分享

wordpress 显示文章日期的方法

WordPress的模板非常灵活,一个优秀的模板所实现的功能往往超出你的想象。这篇文章说说在WordPress里调用文章的发布日期。
收藏 0 赞 0 分享

wordpress 博客优化历程(速度)

自从换了域名,换了主题以后,我一直努力令博客的浏览体验更加好,但因此也挂载了大量的JS文件,页面的载入速度一度变得非常缓慢。于是优化就迫在眉睫了。
收藏 0 赞 0 分享

wordpress 自动在正文内容后添加内容

很多时候,你都需要在文章内容后面添加一些信息,例如订阅,文章分享,收藏和Creative Commons协议声明等。一般情况下,你可以直接编辑主题的single.php文件添加代码来达到目的。
收藏 0 赞 0 分享

开发者应该留意的WordPress2.9新功能小结

WordPress是一款成熟的开源CMS平台,新推出的2.9版本依然保持了以往良好的口碑。这篇文章从WordPress 2.9的codex文档里摘录出几个开发者应该留意的功能进行简析。
收藏 0 赞 0 分享

允许 WordPress 上传任意文件的方法

从 WP 2.8.5 开始文件上传使用预定义的文件扩展名列表过滤,管理员也不例外。
收藏 0 赞 0 分享

WordPress 插件直接将服务器文件导入媒体库

WordPress 插件 直接将服务器文件导入媒体库
收藏 0 赞 0 分享

WordPress博客SEO更加完美的6个技巧

wordpress在搜索引擎优化方面做的也十分不错,在搜索引擎优化方面它基本上给我们解决了80%左右的问题,但余下的20%左右的SEO工作,还是需要人工要去做的,而且也是非常有必要的,怎么才能做好这余下的20%左右的工作呢?
收藏 0 赞 0 分享

WordPress 图片用单独域名储存方法

Yslow 提高网站加载速度里有这么一条:Used Cookie Free Domains , 大意是浏览器会对作用域内每个加载的对象传递 Cookie,在加载图像或者JS、CSS的时候最好用 Cookie-free 域名。如果没有多余的域名可以用一个子域实现,但是要设置 Coo
收藏 0 赞 0 分享
查看更多