以前小站的固定链接好不兽性化,构造是/%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修改固定链接后301重定向的方法》的一些看法。更多内容请查看本栏目更多内容!
概述 最近买了台效劳器,预备搭建集体博客,来继续更新本人的博客, 环境 效劳器操作零碎:CentOS 7.0 ...
很多WordPress站长都常常遭到渣滓评论的滋扰,针对这种状况,采纳上面的代码可能会对你有所协助。将以下代码...
wordpress能够设置自定义字段,不便扩大性能,wordpress利用巧妙的数据库表设计达到这一目的,posts表寄存文...
区分访客国度有什么用? 这里是几个我利用该性能的例子. 1.区分网站性能 这个博客有翻译文章的性能, 这是...
首先到Flash MP3 Player 的主页上下载该顺序。解紧缩之后,将 mp3player.swf 和 ufo.js 两个文件放到效劳器...
在 WordPress 中加载脚本(为 CSS 和 JS,下同)文件,大少数人的做法是间接在 header.php 文件中增加 link...