Webサイト運営で避けて通れないのが.htaccessファイルの設定です。リダイレクト設定や基本認証、セキュリティ強化など、サーバー側の設定を簡単にコントロールできる重要なファイルです。
今回は実務でよく使う.htaccessの基本設定をまとめました。コピペで使える実用的なコードを中心に紹介します。
.htaccessファイルの基本
.htaccessファイルはApacheサーバーの設定をディレクトリ単位で制御するファイルです。サーバールートや特定のディレクトリに配置することで、そのディレクトリ以下に設定が適用されます。
ファイル作成時の注意点
- ファイル名は「.htaccess」(拡張子なし)
- 文字コードはUTF-8(BOMなし)推奨
- 改行コードはLF
- 設定後は動作確認を必ず行う
リダイレクト設定
wwwあり・なしの統一
SEO対策として重要なwwwの統一設定です。
# wwwなしに統一
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# wwwありに統一
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+\.[^.]+)$ [NC]
RewriteRule ^(.*)$ https://www.%1/$1 [R=301,L]
HTTPからHTTPSへの強制リダイレクト
# HTTPS強制リダイレクト
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
特定ページのリダイレクト
# 個別ページのリダイレクト
Redirect 301 /old-page.html https://example.com/new-page.html
# ディレクトリ単位のリダイレクト
RedirectMatch 301 ^/old-directory/(.*)$ https://example.com/new-directory/$1
基本認証(Basic認証)
管理画面やテストサイトのアクセス制限に使用します。
# 基本認証設定
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /path/to/.htpasswd
Require valid-user
.htpasswdファイルの作成例:
# パスワード生成(コマンドライン)
htpasswd -c .htpasswd username
セキュリティ設定
不要なファイルへのアクセス禁止
# 設定ファイルのアクセス禁止
<Files ~ "^\.(htaccess|htpasswd|env)$">
Order allow,deny
Deny from all
</Files>
# バックアップファイルのアクセス禁止
<Files ~ "\.(bak|backup|old|tmp)$">
Order allow,deny
Deny from all
</Files>
特定IPアドレスのブロック
# 特定IPをブロック
Order allow,deny
Allow from all
Deny from 192.168.1.100
Deny from 10.0.0.0/8
ユーザーエージェントによるブロック
# 悪質なボットをブロック
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (bot|crawler|spider) [NC]
RewriteRule .* - [F,L]
パフォーマンス向上設定
Gzip圧縮の有効化
# Gzip圧縮設定
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>
ブラウザキャッシュの設定
# ブラウザキャッシュ設定
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
</IfModule>
WordPress向け設定
管理画面のセキュリティ強化
# wp-adminディレクトリの保護
<Files wp-login.php>
Order allow,deny
Allow from 192.168.1.0/24
Allow from xxx.xxx.xxx.xxx
</Files>
# wp-config.phpの保護
<Files wp-config.php>
Order allow,deny
Deny from all
</Files>
XML-RPCの無効化
# XML-RPC無効化
<Files xmlrpc.php>
Order allow,deny
Deny from all
</Files>
エラーページの設定
# カスタムエラーページ
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
ErrorDocument 403 /403.html
よく使う実用的な設定集
ファイル拡張子の非表示
# .html拡張子を非表示
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
末尾スラッシュの統一
# 末尾スラッシュを削除
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
# 末尾スラッシュを追加
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !.*/$
RewriteRule ^(.*)$ /$1/ [R=301,L]
設定時の注意点
- バックアップを取る:設定前は必ず元ファイルをバックアップ
- 段階的に設定:一度にすべて設定せず、項目ごとに動作確認
- 503エラーに注意:文法エラーがあるとサイトが表示されなくなる
- ModRewriteの確認:サーバーでmod_rewriteが有効か確認
- パスの記載:絶対パスと相対パスを正しく使い分ける
まとめ
.htaccessは正しく設定すればサイトのセキュリティとパフォーマンスを大幅に向上させることができます。今回紹介した設定をベースに、自分のサイトに必要な項目を選んで実装してみてください。設定後は必ずブラウザでの動作確認を忘れずに行いましょう。
