問題描述

我最近注意到,當 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。