htaccess,分页,缺页,重定向到主页类别

时间:2011-06-29 08:14:47

标签: apache .htaccess mod-rewrite redirect

我有一个WordPress安装,几个月前我决定改变主题。新主题需要将每页的总帖子数从第一个主题中的8个更改为下一个主题中的10个,因此预期的新主题没有预览主题的所有页面。

问题是,现在Google正在查找页面/存档/ tag / tag-name / page / 5,而该页面不存在。

我需要做的是制作一个重写规则,对于页面不存在的情况,可以在一个URL中的根标记或类别页面上使用307(临时移动)重定向,该URL看起来像/ archives /标签/标签名/

在我的htaccess中我尝试过:

    RewriteEngine On     RewriteBase /

# Check if any tag has a page that not exists 
# and redirect to first page of this category
RewriteCond ^archives/tag/.*/page/[0-9]+(/?)$ !-f
RewriteRule ^archives/tag/(.*)/page/[0-9]+(/?)$ http://www.my-site.ext/archives/tag/$1/ [L,R=307]

# Check if any category has a page that not exists 
# and redirect to first page of this category
RewriteCond ^archives/category/.*/page/[0-9]+(/?)$ !-f
RewriteRule ^archives/category/(.*)/page/[0-9]+(/?)$ http://www.my-site.ext/archives/category/$1/ [L,R=307]

# Default wordpress rewrite rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

但上述规则也会在第一页

中重定向现有页面

请帮忙吗? :)

1 个答案:

答案 0 :(得分:1)

我认为您需要做的是在WordPress级别处理,而不是Apache。

在你的主题的functions.php文件中,尝试挂钩“template_redirect”动作,如下所示:

function nikos_redirect_paginated_404() {
  if (is_404() && [other conditions]) {
    wp_redirect($some_better_url, '307');
    exit;
  }
}

add_action('template_redirect', 'nikos_redirect_paginated_404');

您必须自己为[other conditions]$some_better_url找出正确的逻辑。

(这个一般的想法是基于wp_old_slug_redirect函数,可以在WordPress的query.php脚本中找到)

相关问题