如何在Wordpress中重定向不再存在的页面?

时间:2015-04-29 07:50:55

标签: php wordpress .htaccess redirect

当我第一次创建Wordpress网站时,我并不知道在链接中使用slugs而不是ids是个好主意。我当时有一个页面有地址:

http://example.com/?page_id=50

当我在我的国家为一个流行的电子商务平台制作模板时,我包含了所述链接。一年后,该网站崩溃,我不得不重建它。不用说,有问题的页面被分配了不同的ID。我无法在电子商务平台上进行更改(这需要进行数千次个别更改 - 这并不是夸大其辞)。

那么,我可以以某种方式将/?page_id=50重定向到其他页面吗?问题是没有页面具有此特定ID。

有什么办法吗?我试过.htaccess重定向,但它没有工作或者我做错了什么。 我写了

Redirect /?page_id=50 http://example.com/page-slug/

在.htaccess中

我也尝试过(如下所示):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)page_id=50($|&)
RewriteRule . http://example.com/page-slug/? [R=301,L]

以及:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{QUERY_STRING} (^|&)page_id=50($|&)
RewriteRule . http://lucart.pl/lucart-w-radio-eska/? [R=301,L]

</IfModule>

# END WordPress

既没有奏效:(

请帮忙。

2 个答案:

答案 0 :(得分:2)

请在html开始之前在主题header.php模板的最顶部添加此重定向php代码:

$request_url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    if($request_url=='http://example.com/?page_id=50'){
        wp_redirect('http://example.com/page-slug/ ');
    }

答案 1 :(得分:1)

您不能将查询字符串与Redirect一起使用。您需要使用mod_rewrite并检查query string

RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)page_id=50($|&)
RewriteRule . http://example.com/page-slug/? [R=301,L]
相关问题