WordPress用の.htaccess設定

WordPress

レンタルサーバーなどでブログを運営しているとセキュリティの問題についても考えなければなりません。自分のブログが改竄されるだけでなく、他のブログをサイバー攻撃する踏み台にされてしまい、その結果加害者になってしまう恐れもあるからです。

そこで、WordPressのセキュリティ強化向け .htaccess 設定をまとめました。

構文の基礎知識

ディレクティブ

ディレクティブには以下の3種類があり、これらと後述する判定文を組み合わせてアクセス許可や拒否を指定します。

<RequireAny>...</RequireAny>条件が一つでも当てはまれば、アクセスを許可する
<RequireAll>...</RequireAll>全ての条件が当てはまれば、アクセスを許可する
<RequireNone>...</RequireNone>条件が一つでも当てはまれば、アクセスを拒否する

判定文

Require ip xx.xx.xx.xxIPアドレスが xx.xx.xx.xx の場合、アクセスを許可する
Require not ip xx.xx.xx.xxIPアドレスが xx.xx.xx.xx ではない場合、アクセスを許可する
Require host example.jpホスト名が example.jp の場合、アクセスを許可する
Require not host example.jpホスト名が example.jp ではない場合、アクセスを許可する
Require all granted全てのアクセスを許可する
Require all denied全てのアクセスを拒否する

注意点その1

以下のように RequireAll ディレクティブのブロック内で Require not ip や Require not host だけを使うとエラーが出るので Require all granted を付ける必要があります。

# 間違っている構文
<RequireAll>
    Require not ip xx.xx.xx.xx
    Require not host example.jp
</RequireAll>
# 正しい構文
<RequireAll>
    Require all granted
    Require not ip xx.xx.xx.xx
    Require not host example.jp
</RequireAll>

注意点その2

RequireNone ディレクティブは RequireAll ディレクティブのブロック内でのみ使用可能です。

# 正しい構文
<RequireAll>
    Require all granted
    <RequireNone>
        Require ip xx.xx.xx.xx
    </RequireNone>
</RequireAll>

注意点その3

RequireNone ディレクティブのブロック内で Require not … は使用不可です。

# 間違っている構文
<RequireAll>
    Require all granted
    <RequireNone>
        Require not ip xx.xx.xx.xx
    </RequireNone>
</RequireAll>

注意点その4

RequireAny ディレクティブのブロック内に Require all granted は不要です。RequireAny は条件が一つでも一致したらアクセスを許可するディレクティブなので Require all granted を指定しても意味がありません。

アクセス制限 (セキュリティ対策)

wp-config.php へのアクセス禁止

<Files wp-config.php>
    Require all denied
</Files>

wp-login.php へのアクセス制限

xx.xx.xxのところにはアクセスを許可したいIPアドレスを入れてください。

<Files wp-login.php>
    Require all denied
    Require ip xx.xx.xx.xx
</Files>

ワームからのアクセスを拒否

SetEnvIf Request_URI "^/(mem_bin|_mem_bin|vti_bin|_vti_bin|c|d|msadc|MSADC|scripts|default\.ida|NULL\.IDA|cmd\.exe|root\.exe)" worm
Require not env worm

悪質なボットからのアクセスを拒否

BrowserMatchNoCase (nabot|dloader|digext|openbot|archiver|crawler|spider|scooter|zyborg) refuse_malicious_bots
Require not env refuse_malicious_bots

.htaccess .htpasswd .bak .BAK へのアクセスを拒否

<FilesMatch "(^\.ht|~$|\.bak$|\.BAK$)">
    Require all denied
</FilesMatch>

WordPressの重要なファイルへのアクセスを拒否

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^wp-admin/includes/ - [F,L]
    RewriteRule !^wp-includes/ - [S=3]
    RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
    RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
    RewriteRule ^wp-includes/theme-compat/ - [F,L]
</IfModule>

指定した接続元からのアクセスのみ許可

IPアドレス指定

次の例では IPアドレスが xx.xx.xx.xx の場合のみアクセスを許可しています。

<RequireAny>
    Require ip xx.xx.xx.xx
</RequireAny>

ドメイン指定

以下の例では、example.jp というドメインからのアクセスのみ許可しています。

<RequireAny>
    Require host example.jp
</RequireAny>

指定した接続元からのアクセスを拒否

IPアドレス指定

以下は、IPアドレスが xx.xx.xx.xx の場合にアクセスを拒否します。

<RequireAll>
    Require all granted
    Require not ip xx.xx.xx.xx
</RequireAll>

以下のように書くこともできます。

<RequireAll>
    Require all granted
    <RequireNone>
        Require ip xx.xx.xx.xx
    </RequireNone>
</RequireAll>

ドメイン指定

以下の例では、example.jp というドメインからのアクセスを拒否しています。

<RequireAll>
    Require all granted
    Require not host example.jp
</RequireAll>

以下のように書くこともできます。

<RequireAll>
    Require all granted
    <RequireNone>
        Require host example.jp
    </RequireNone>
</RequireAll>

その他の役立つhtaccess設定

画像への直接リンク禁止

以下の例では「https://www.xxx.yy」というドメインのサイト上にある gif、png、jpg 画像への直接リンクを禁止しています。

RewriteBase /
Options FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} ^(.*)\.(gif|png|jpg)$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.xxx\.yy/.*$ [NC]
RewriteRule ^(.*)$ - [F]

301リダイレクト

301リダイレクトについては、こちらの記事をご覧ください。

httpをhttpsに301リダイレクトする
レンタルサーバーではSSLを利用できるところも多いと思います。しかし、SSLを有効化した後、忘れてはいけない設定があります。それは http へのアクセスを https に301リダイレクトさせることです。そもそも301リダイレクトとは?分...

参考にさせていただいたサイト様

極力危機を回避するための!【WordPressセキュリティ対策まとめ】 | LINKSTORY LIBRARY
目次wp-config.phpへの対策wp-config.phpへのアクセスを不可にするwp-config.php を1つ上の階層(非公開ディレクトリ)へ移動wp-login.phpにアクセス制限をか
あなたのホームページは大丈夫?WordPressのセキュリティを高めるためにやっておきたい7つのこと
今回は、WordPressのセキュリティを高めるために2017年にやっておきたい対策方法を7つのポイントに絞ってご紹介していきます。何も対策せずにWordPressで作成したホームページを放置しておくのは、鍵を開けたまま家を外出するのと同じ...
apache2.4でのアクセス制御はAllowではなくRequireを使おう
Apache2.2で利用されていallow, denyなどの構文はApache 2.4では非推奨で代わりにRequireを使います。 許可するIPを記述するには 拒否するIPを記述するには 目次 Apache2.4ではR ...
リモートホストによるアクセス制御 - ADAMレンタルウェブ
月額209円からの高機能レンタルサーバー【ADAMレンタルウェブ】は、マルチドメイン、無料SSL、PHP、MySQLに対応し、WordPressなどの各種CMSも快適に動作します。

コメント