開発段階などで、WordPress でサイトを非公開にする場合のメモ。
検索エンジンにインデックスさせないようにするには、以下を行う。
インデックスできないように設定した場合、以下のような meta タグが挿入される。
<meta name='robots' content='noindex,nofollow' />
サイト全体にベーシック認証を設定してユーザーから閲覧できないようにするには、以下を WordPress の .htaccess に記述して、パスワードファイルを作成して安全な位置に配置する。
AuthName "Login" AuthType Basic AuthUserFile /home/xxxx/.htpasswd require valid-user
パスワードファイルの作成などに関しては、「WP インストール時の注意点とセキュリティ」の「ログインにベーシック認証を使用」を参照。
サイト全体に BASIC 認証をかける場合、前述のほかに、以下のプラグインを利用する方法がある。
プラグイン WP Basic Auth
WordPress サイトに Basic 認証をかけるプラグイン「WP Basic Auth」(作者のページ)
インストールして有効化すると管理画面と静的ファイル以外を閲覧する時に Basic 認証でアクセス制限がかかるようになっているので、ページにアクセスすると、ユーザー名とパスワードが求められる。またユーザー名とパスワードが、WordPress管理画面のユーザー名とパスワードと同じなので開発段階で使用するには便利。
但し、画像ファイルなどにはアクセス制限がかからない(画像のURLを直接入力されると表示される)ので注意が必要。
また、マルチサイト(サブディレクトリ形式)で試したところ、ローカル環境では問題がなかったがサーバーの環境では以下のようなエラーがでて使用できなかった。(サーバー側の問題か?)
Notice: Undefined offset: 1 in /home/xxxx/public_html/wp/wp-content/plugins/wp-basic-auth/plugin.php on line 74
Warning: Cannot modify header information – headers already sent by (output started at /home/xxxx/public_html/wp/wp-content/plugins/wp-basic-auth/plugin.php:74) in /home/xxxx/public_html/wp/wp-content/plugins/wp-basic-auth/plugin.php on line 83
    Warning: Cannot modify header information – headers already sent by (output started at /home/xxxx/public_html/wp/wp-content/plugins/wp-basic-auth/plugin.php:74) in /home/xxxx/public_html/wp/wp-content/plugins/wp-basic-auth/plugin.php on line 84
    Authorization Required
.htaccess でのベーシック認証が使えない場合等、以下のような方法もある(但し、面倒くさい)。
以下を functions.php に記述すると、
以下は注意点
//公開 URL にアクセスすると、WordPress のログイン画面にリダイレクト
function valid_login() {
  if(!is_user_logged_in()) {
    auth_redirect();
  }
}
add_action('template_redirect', 'valid_login');
//ログインページの変更
  define( 'ANYWHERE_LOGIN_PAGE', 'my-login.php' );
  add_action( 'login_init', 'anywhere_login_init' );
  add_filter( 'site_url', 'anywhere_login_site_url', 10, 4 );
  add_filter( 'wp_redirect', 'anywhere_login_wp_redirect', 10, 2 );
  
  if ( ! function_exists( 'anywhere_login_init' ) ) {
    function anywhere_login_init() {
      if ( !defined( 'ANYWHERE_LOGIN' ) || sha1( 'keyword' ) != ANYWHERE_LOGIN ) {
        status_header( 403 );
        exit;
      }
    }
  }
  if ( ! function_exists( 'anywhere_login_site_url' ) ) {
    function anywhere_login_site_url( $url, $path, $orig_scheme, $blog_id ) {
      if ( $path == 'wp-login.php' && ( is_user_logged_in() || strpos( $_SERVER['REQUEST_URI'], ANYWHERE_LOGIN_PAGE ) !== false ) )
        $url = str_replace( 'wp-login.php', ANYWHERE_LOGIN_PAGE, $url );
        return $url;
     }
  }
  if ( ! function_exists( 'anywhere_login_wp_redirect' ) ) {
    function anywhere_login_wp_redirect( $location, $status ) {
      if ( strpos( $_SERVER['REQUEST_URI'], ANYWHERE_LOGIN_PAGE ) !== false )
        $location = str_replace( 'wp-login.php', ANYWHERE_LOGIN_PAGE, $location );
        return $location;
      }
  }
//ベーシック認証  
add_action( 'login_init', 'basic_auth' );
function auth_login_init() {
  if ( ! is_user_logged_in() ) {
    basic_auth(); 
    }
  }
  
function basic_auth(){
  $hashed_user = "B.7zvNlp.z/Pxy";  //暗号化されたユーザー名
  $hashed_password = "10Lh53LdzKN1KZ";  //暗号化されたパスワード
  if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){
    if (crypt($_SERVER['PHP_AUTH_USER'], $hashed_user) == $hashed_user && crypt($_SERVER['PHP_AUTH_PW'], $hashed_password) == $hashed_password){
      return;
    }
  }
  header('WWW-Authenticate: Basic realm="Restricted Area"');
  header('HTTP/1.0 401 Unauthorized');
  header('Content-type: text/html; charset='.mb_internal_encoding());
  die("Authorization Failed. Contact your administrator."  );
}