こんにちは!シミダイ(@shimidai2100)です。
.htaccessといえばホスティングサービスを利用する上で必須の設定ファイルになります。
.htaccessとは?
「.htaccess」とは、最も多く使用されているオープンソースのWEBサーバー「Apache」を制御する設定ファイルの1つです。
Apacheの制御は設定ファイル「httpd.conf」に記載されていますが、これをWEBサーバー全体の制御を行っているため、サーバー管理者しか変更することができません。
Xserverなどの共有サーバ構成のホスティングサービスでWEBサーバーの設定を変えたい場合は、「.htaccess」でWEBサーバの挙動を制御することができます。
.htaccessで出来ることとは?
「.htaccess」で出来ることは以下になります。
- リダイレクト設定(違うページに移動させる、URL変換を行う)
- アクセス制御(アクセス出来るファイル・ディレクトリを制限する)
- チューニング(ファイル転送容量を減らす)
- その他設定変更
.htaccessの作成方法
「.htaccess」の作成方法はとても簡単で、テキストファイルで「.htaccess」ファイルを作成すればOKです。
ファイル名の前にドットがついており、隠しファイルにすることを忘れずに行ってください。
作成したファイルを制御を掛けたいディレクトリに配置させます。
一般的にはindex.htmlやindex.phpファイルと同一ディレクトリに配置します。
リダイレクト設定
リダイレクト設定は「.htaccess」で最も頻繁に利用します。
ドメイン名をwww”あり”に統一
Yahoo!JapanやYoutubeなどのようにwww付きのドメインにする場合には↓の記載します。
1 2 3 4 5 |
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] </IfModule> |
ドメイン名をwww”なし”に統一
個人のブログや中小企業などに多い、www無しのドメインにする場合には↓の記載します。
1 2 3 4 5 |
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L] </IfModule> |
HTTPS(SSL)アクセスに変更
HTTPS(SSL)アクセスに変更する場合は、↓のように記載します。
HTTPSアクセス化はSEO観点で必須なので必ず設定しておきましょう。
1 2 3 4 5 |
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] </IfModule> |
301(恒久的)リダイレクト
サイトの引っ越しなどで使用する301(恒久的)リダイレクト設定は↓になります。
1 2 3 4 5 |
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ index.html [R=301] RewriteRule ^(.*)$ https://%{HTTP_HOST}/index.html </IfModule> |
302(暫定的)リダイレクト
サイトのメンテナンス時などで使用する302(暫定的)リダイレクト設定は↓になります。
1 2 3 4 5 |
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ index.html [R=302] RewriteRule ^(.*)$ https://%{HTTP_HOST}/index.html </IfModule> |
アクセス制御
アクセスさせたくないファイルやディレクトリがある場合には、アクセス制御を行います。
ベーシック認証
最も安価にアクセス制御を行う方法として「ベーシック認証」を利用できます。
1 2 3 4 5 |
AuthUserFile /var/www/.htpasswd AuthGroupFile /dev/null AuthName "Please input UserID and Password." AuthType Basic require valid-user |
許可リスト(ホワイトリスト)での制御
「アクセス許可していないが、○○だけは許可する」という場合には↓のように設定します。
「Allow(許可)」に対象のIPアドレスやホスト名を記載します。
1 2 3 |
Order Allow,Deny Allow from XXX.XXX.XXX.XXX Allow from ホスト名 |
「Order」や「Allow」は後方に記載されたものが優先的に使用されます。↑の例では「Deny(拒否)」が優先的に使用されます。
「.htaccess」は後方に記載されたものが優先と覚えておきましょう。
拒否リスト(ブラックリスト)での制御
「アクセスを許可しているが、○○だけは拒否する」という場合には↓のように設定します。
「Deny(拒否)」に対象のIPアドレスやホスト名を記載します。
1 2 3 |
Order Deny,Allow Deny from XXX.XXX.XXX.XXX Deny from ホスト名 |
「Order」や「Deny(拒否)」は後方に記載されたものが優先的に使用されます。↑の例では「Aloow(許可)」が優先的に使用されます。
「.htaccess」は後方に記載されたものが優先と覚えておきましょう。
チューニング
「.htaccess」でのチューニングは、「ブラウザ側でのキャッシュ」と「データの圧縮転送」の2つになります。
ブラウザ側でのキャッシュ
ブラウザ側にファイルをキャッシュさせるには「mod_expires.c」を使用します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<IfModule mod_expires.c> ExpiresActive on ExpiresDefault "access plus 1 days" # HTML ExpiresByType text/html "access plus 1 hours" # CSS ExpiresByType text/css "access plus 1 days" # JavaScript ExpiresByType application/javascript "access plus 1 days" ExpiresByType application/x-javascript "access plus 1 days" ExpiresByType text/javascript "access plus 1 days" # Image & Movie ExpiresByType audio/ogg "access plus 1 days" ExpiresByType image/bmp "access plus 1 days" ExpiresByType image/gif "access plus 1 days" ExpiresByType image/jpeg "access plus 1 days" ExpiresByType image/png "access plus 1 days" ExpiresByType image/svg+xml "access plus 1 days" ExpiresByType image/webp "access plus 1 days" ExpiresByType video/mp4 "access plus 1 days" ExpiresByType video/ogg "access plus 1 days" ExpiresByType video/webm "access plus 1 days" # Favicon ExpiresByType image/vnd.microsoft.icon "access plus 1 days" ExpiresByType image/x-icon "access plus 1 days" # Fonts # Embedded OpenType (EOT) ExpiresByType application/vnd.ms-fontobject "access plus 1 month" ExpiresByType font/eot "access plus 1 month" # OpenType ExpiresByType font/opentype "access plus 1 month" # TrueType ExpiresByType application/x-font-ttf "access plus 1 month" # Web Open Font Format (WOFF) 1.0 ExpiresByType application/font-woff "access plus 1 month" ExpiresByType application/x-font-woff "access plus 1 month" ExpiresByType font/woff "access plus 1 month" # Web Open Font Format (WOFF) 2.0 ExpiresByType application/font-woff2 "access plus 1 month" # Other ExpiresByType text/x-cross-domain-policy "access plus 1 days" </IfModule> |
キャッシュさせる期間は↓を参考にしてみてください。
データの圧縮転送
データを圧縮してから転送することでクライアント側に送信するデータ量を抑えることが出来ます。
ネットワークがボトルネックに場合に利用することをオススメします。
データ圧縮には「mod_deflate.c」を使用します。
1 2 3 4 5 6 7 8 9 10 11 |
<ifModule mod_deflate.c> <IfModule mod_filter.c> AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain AddOutputFilterByType DEFLATE image/svg+xml application/xhtml+xml application/xml AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-otf AddOutputFilterByType DEFLATE font/truetype font/opentype </IfModule> </ifModule> |
また別の方法として「mod_pagespeed」を使用する方法があります。
ただしXserverなどの一部の共有サーバでしか使用できません。
1 2 3 |
<IfModule pagespeed_module> ModPagespeed on </IfModule> |
その他の設定
その他のよく使う設定です。「httpd.conf」で
400系エラーページの指定
400系エラーページを「ErrorDocument 」を使用して個別に指定することが出来ます。
1 2 3 4 5 6 7 8 |
# 400 Bad Request ErrorDocument 400 /400error.html # 401 Unauthorixed ErrorDocument 401 /401error.html # 403 Forbidden ErrorDocument 403 /403error.html # 404 Not Found ErrorDocument 404 /404error.html |
500系エラーページの指定
サーバサイドのエラーである500系も400系と同様に「ErrorDocument 」を使用して個別に指定することができます。
1 2 3 4 5 6 |
# 500 Internal Server Error ErrorDocument 500 /500error.html # 502 Bad Gateway ErrorDocument 502 /502error.html # 503 Service Unavailable ErrorDocument 503 /503error.html |
ファイル・ディレクトリ一覧を非表示にする
ファイル・ディレクトリ一覧上方を非表示させます。セキュリティ観点で非表示させることが一般的です。
1 2 |
# Disable index map Options -Indexes |
CGIの実行を有効化する
CGIの有効化も「.htaccess」で制御することができます。
実行可能にするCGIの拡張子は「AddType」で設定します。
1 2 3 4 |
# Enable Excute CGI Options +ExecCGI AddHandler cgi-script cgi AddType application/x-httpd-cgi .cgi .pl .py .rb |
その他必要な記述を更新してきます!