關閉附件頁面及編輯robots.txt

雖然流量還在安全閾值內,但這樣白白浪費也不是個辦法。

關閉附件頁面

WP 6.4後已無此需要,詳情:Changes to attachment pages

WordPress預設會幫附件獨立建一個頁面,這應該是服務於那些有商品展示需要的功能,對我這種迷你型的部落格只是冗餘。

有三種方式關閉該功能。

  • 外掛
    • All in One SEO Pack
    • Yoast SEO
    • Attachment Pages Redirect
  • 編輯functions.php
  • 新增image.php

外掛我個人是敬謝不敏,functions.php又稍嫌複雜了點。

於是乎——

在模板資料夾新增image.php,並輸入:

How disable image attachment pages in WordPress?

https://stackoverflow.com/a/38079027
<?php
global $post;
if ( $post && $post->post_parent ) {
    wp_redirect( get_permalink( $post->post_parent ), 301 );
} else {
    wp_redirect( home_url( '/' ), 301 );    
}
exit;
  • 該功能為當附件有關聯文章時便轉至該文章,若無,則轉至首頁。
  • 比起其他網路流傳的程式碼,此方案避免了當附件為單獨存在,沒有相關聯文章時會無限轉址的錯誤。

完成。

編輯robots.txt

直接使用Mitchell Krog(mitchellkrogza)所提供的黑名單:apache-ultimate-bad-bot-blocker

試試多少擋掉這些空耗流量的機器人。