李建中 發表於 2023-10-20 00:00:00

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

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
以前小站的固定链接好不人性化,结构是/%year%/%monthnum%/%postname%/,这样看起来又不直观又长的,今天做了一个301重定向,然后修改了新的固定链接,现在的固定链接格式是/%postname%.html,这样看起来短了好多,也直观了好多,<br>
不过小站已经被搜索引擎收录了好多,以前的地址都无法访问了,所以要做好301重定向,跳转到新的固定链接所生成的地址中去.要不然都是404就悲剧了.下面贴出解决办法</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode1" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
$rewrite_config = array();<br>
$rewrite_config['highpriority'] = true ;<br>
$rewrite_config['rewrite'] = array();<br>
$rewrite_config['oldstructure'] = "/%year%/%monthnum%/%postname%/";<br><br>
function wpdaxue_pm_the_posts($post) {<br>
global $wp;<br>
global $wp_rewrite;<br>
global $rewrite_config;<br><br>
$rewrite_config['rewrite'] = $wp_rewrite-&gt;generate_rewrite_rule($rewrite_config['oldstructure'], false, true, true, true);<br>
if ($post != NULL &amp;&amp; is_single() &amp;&amp; $rewrite_config['oldstructure'] != $wp_rewrite-&gt;permalink_structure) {<br>
if (array_key_exists($wp-&gt;matched_rule, $rewrite_config['rewrite'])) {<br>
// ok, we need to generate a 301 Permanent redirect here.<br>
header("HTTP/1.1 301 Moved Permanently", TRUE, 301);<br>
header('Status: 301 Moved Permanently');<br>
$permalink = get_permalink($post-&gt;ID);<br>
if (is_feed()) {<br>
$permalink = trailingslashit($permalink) . 'feed/';<br>
}<br>
header("Location: ". $permalink);<br>
exit();<br>
}<br>
}<br>
return $post;<br>
}<br><br>
function wpdaxue_pm_post_rewrite_rules($rules) {<br>
global $wp_rewrite;<br>
global $rewrite_config;<br>
$oldstruct = $rewrite_config['oldstructure'];<br><br>
if ($oldstruct != NULL &amp;&amp; $oldstruct != $wp_rewrite-&gt;permalink_structure) {<br>
$rewrite_config['rewrite'] = $wp_rewrite-&gt;generate_rewrite_rule($oldstruct, false, true, true, true);<br>
if ($rewrite_config ['highpriority'] == true) {<br>
return array_merge($rewrite_config['rewrite'], $rules);<br>
} else {<br>
return array_merge($rules, $rewrite_config['rewrite']);<br>
}<br>
}<br>
return $rules;<br>
}<br>
add_filter('the_posts', 'wpdaxue_pm_the_posts', 20);<br>
add_filter('post_rewrite_rules', 'wpdaxue_pm_post_rewrite_rules');</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
将我的$rewrite_config['oldstructure'] = “/%year%/%monthnum%/%postname%/”后面的/%year%/%monthnum%/%postname%/修改成你自己的旧的固定链接格式,然后将这段代码加入到主题的function.php中,然后设置成新的固定链接格式就搞定了.</p>
頁: [1]
查看完整版本: wordpress修改固定链接后301重定向的方法