问题描述

我最近注意到,当 WordPress 未完整提交时,WordPress 尝试使用 auto-complete 网址。例如。我有一个帖子网址,如下所示:

http://www.mysite.com/some-post-title

如果我浏览到以下 URL:

http://www.mysite.com/some-post-ti

我可以看到 URL 提交到 WordPress,但 WordPress 正在做一个 301 重定向到 http://www.mysite.com/some-post-title

如何禁用此行为?

最佳解决方案

我相信是 redirect_canonical 功能挂钩到 template_redirect 。您应该能够禁用它:

remove_filter('template_redirect', 'redirect_canonical'); 

但是您应该考虑是否要这样做,因为它相当复杂,并且执行一些重要的 SEO 功能:

Redirects incoming links to the proper URL based on the site url.

Search engines consider www.somedomain.com and somedomain.com to be two different URLs when they both go to the same location. This SEO enhancement prevents penalty for duplicate content by redirecting all incoming links to one or the other.

Prevents redirection for feeds, trackbacks, searches, comment popup, and admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7, page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST requests.

Will also attempt to find the correct link when a user enters a URL that does not exist based on exact WordPress query. Will instead try to parse the URL or query in an attempt to figure the correct page to go to.

http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/canonical.php#L13

以下可能会杀死自动完成,而不会干扰 SEO 组件,但我无法承诺。代码几乎没有被测试,因为我从来没有想要禁用它。我真的要学习 redirect_canonical 来确定什么。

function kill_404_redirect_wpse_92103() {
  if (is_404()) {
   add_action('redirect_canonical','__return_false');
  }
}
add_action('template_redirect','kill_404_redirect_wpse_92103',1);

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。