你應該在WordPress自身做到這一點(在矛盾.htaccess
),因爲您必須根據該名稱獲取真正的永久鏈接。
請嘗試更新您的固定鏈接到新的結構,並將以下代碼添加到您的functions.php文件。
<?php
function redirect_postname_to_date_structure($wp) {
$pattern = '#^([^/]+)\.html$#';
$matches = array();
if (preg_match($pattern, $wp->request, $matches)) {
$slug = $matches[1];
$args = array(
'name' => $slug,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1
);
// Try to retrieve post based on slug
$posts = get_posts($args);
if ($posts) {
$permalink = get_permalink($posts[0]->ID);
wp_redirect($permalink, 301);
exit;
}
}
}
add_action('parse_request', 'redirect_postname_to_date_structure');
?>
PS:我建議你在第一次測試使用302個狀態碼(在wp_redirect()
)。當你確信它可以工作時,你可以切換到301。
你好。我現在修復。首先,我有一個錯誤,因爲我忘了從.htaccess刪除以前的301重定向一旦我revome,工作正常。我檢查谷歌舊鏈接和完成重新定向到新的永久鏈接完美沒有問題。謝謝你這麼多 – user2143527 2013-03-15 09:37:26
@ user2143527我很高興你得到它的工作。請標記我的答案,如果它幫助你。 – kjetilh 2013-03-15 11:46:49
Hiya。我不能投票,因爲我的新和系統說我需要15聲望...抱歉。但是一旦我獲得了我需要的聲望,我就會投票。 – user2143527 2013-03-16 10:20:21