WordPress固定链接修改后出现404
WordPress固定链接修改后,文章帖子出现HTTP 404的错误,找不到网页。例如我的博客之前使用的固定链…
WordPress固定链接修改后,文章帖子出现HTTP 404的错误,找不到网页。例如我的博客之前使用的固定链接为/2018/01/wordpress.html,但现在想改成/wordpress/wordpress,直接修改新链接可以起效果,但是通过之前的链接访问全部是404,找不到文章。这对搜索引擎的收录肯定不好,所以想把以前的链接全部301重定向到新的链接上去~
Nginx配置
如果是第一次修改固定链接就找不到,那么是因为没有在Nginx中加入对应的rewrite规则,修改对应的配置文件,加上如下配置即可:
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; } }
修改固定链接
如果是跟我一样,修改了固定链接,由原本的https://www.flyzy2005.cn/2017/12/build-shadowsocks-on-vps.html,修改成了https://www.flyzy2005.cn/fan-qiang/shadowsocks/build-shadowsocks-on-vps,那么需要在functions.php中加入对应的重定向规则,代码如下:
#固定链接301重定向 $rewrite_config = array(); $rewrite_config['highpriority'] = true; $rewrite_config['rewrite'] = array(); $rewrite_config['oldstructure'] = "/%year%/%monthnum%/%postname%.html"; 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; }
其中$rewrite_config[‘oldstructure’]改成你对应的原固定链接即可实现重定向~
注:修改functions.php后,需要在固定链接里点击保存更改刷新配置!!!
类别:WordPress入门、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!