2017-08-28 24 views
1

我希望鏈接如:https://www.mywordpresssite.com/2017/08/28被重寫並重定向到:https://www.mywordpresssite.com/date-archives/2017/08/28。想法是將這些「日期」鏈接放在robots.txt中,因此如果鏈接中有「date-archives」,我將能夠編寫由robots.txt支持的簡單正則表達式。在.htaccess文件中重定向「日期」鏈接

這是我試過到目前爲止:

RewriteRule ^/date-archives/([0-9]+)/([0-9]+)/([0-9]+)/ https://www.mywordpressite.com/$1/$2/$3 [R=301,L] 

所以,如果有人試圖去www.mysite.com/2017/08/28它應該被重定向到www.mysite.com/date-archives/2017/08/28和頁面應該打開相同的內容,www.mysite.com/2017/08/28頁面會。

編輯: 我做插件,用來處理重寫:

<?php 
/** 
    *Plugin Name: SEO-date-archives-pages 
*Plugin UIR: 
    *Author: VP 
*/ 
add_action('init', 'rewrite_rule_example'); 
function rewrite_rule_example() { 
    add_rewrite_rule('^date-archives/([0-9]+)/([0-9]+)/([0-9]+)', '^([0- 
9]+)/([0-9]+)/([0-9]+)', 'top'); 

} 

>

我是新來的wordpress真的讓我很困惑在哪裏的問題

回答

1

實際上要重定向反向? 。

您可以使用此規則爲你的第一個規則:

RewriteEngine On 

RewriteRule ^\d{4}/\d{2}/\d{2}/?$ /date-archives/$0 [R=301,L,NE] 

# remaining Wordpress rules go below this 

你重寫規則:

<?php 
/** 
    *Plugin Name: SEO-date-archives-pages 
*Plugin UIR: 
    *Author: VP 
*/ 
function rewrite_rule_example() { 
    add_rewrite_rule('^date-archives/(\d+/\d+/\d+)/?$', '$matches[1]', 'top');  
} 
add_action('init', 'rewrite_rule_example'); 

?> 
+0

謝謝,只是需要把括號重寫規則^(\ d {4}/\ d {2}/\ d {2} /)?$/date-archives/$ 1 [R = 301,L,NE]。順便說一句,你知道在wordpress中是否可以更改生成的「日期鏈接」,所以我根本不必使用重定向? –

+0

對不起,它似乎不起作用。重定向的作品,但我得到404,而不是從「原始」頁面的內容 –

+0

日期沒有這種選項。只適用於「發佈」頁面。但我不明白爲什麼重寫不起作用。如果重寫正確,重寫鏈接應表現爲「真實」鏈接 –