7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Wordpress】WordpressでSSL対応、Basic認証などで.htaccessに記載するアレコレ覚書【.htaccess】

Last updated at Posted at 2016-08-23

SSL化(全ページ)

.httaccess
<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{HTTPS} off
	RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>

ただしさくらサーバの場合

.htaccess

# sakura ssl
<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{ENV:HTTPS} !^on$
	RewriteCond %{HTTP:X-SAKURA-FORWARDED-FOR} ^$
	RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>

に加えて

wp-config.php
// さくらサーバでhttps対応する場合記述
if( isset($_SERVER['HTTP_X_SAKURA_FORWARDED_FOR']) ) {
	$_SERVER['HTTPS'] = 'on';
	$_ENV['HTTPS'] = 'on';
	$_SERVER['HTTP_HOST'] = 'xxx.jp';
	$_SERVER['SERVER_NAME'] = 'xxx.jp';
	$_ENV['HTTP_HOST'] = 'xxx.jp';
	$_ENV['SERVER_NAME'] = 'xxx.jp';
}

を追記、これしないとセカンド以降がSSL化されへんなー。

管理画面にbasic認証

.htaccess
AuthUserFile /xxxx/xxxx/.htpasswd
AuthType Basic
AuthName "Web access"
Satisfy all
Order deny,allow

# 公開前で全体にかける場合は記述、本番公開時コメントアウト
require valid-user

<Files "wp-login.php">
# 本番公開時コメントアウト解除でログイン画面のみ
#require valid-user
</Files>

<FilesMatch "\/?wp-admin">
require valid-user
</FilesMatch>

# admin-ajax.phpだけ除外
<Files "wp-admin/admin-ajax.php">
Satisfy any
order allow,deny
allow from all
</Files>

他ブルートフォースアタック対応など

.htaccess
<Files "xmlrpc.php">
Order Deny,Allow
Deny from all
</Files>

<FilesMatch "^(wp-config\.php|wp-mail\.php|install\.php)">
Order Allow,Deny
Deny from all
</FilesMatch>
7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?