サイトやブログの運営でよく使いそうな.htaccessの設定のまとめ
Post on:2011年6月1日
sponsors
ウェブサイトやブログの運営でよく使いそうな便利な.htaccessの設定を紹介します。
こういうまとめは定期的にあがってきますが、やっぱり必要なのでシェアします。

10 useful .htaccess snippets to have in your toolbox
[ad#ad-2]
下記は各ポイントを意訳したものです。
- URLからwwwを削除
- hotlinkingの防止
- feedをfeedbunnerにリダイレクト
- カスタムエラーページ
- ダウンロードファイルの処理
- PHPのエラーのログ
- URLからファイルの拡張子を削除
- ディレクトリのファイルリストを見せない
- ファイルを圧縮して軽量化
- 文字コードの指定
URLからwwwを削除
SEOなどの理由で、URLからwwwを削除して使うことがあるかもしれません。このスニペットは、あなたのウェブサイトにwww付きでアクセスしてきてもwww無しに向け直します。
1 2 3 | RewriteEngine On RewriteCond %{HTTP_HOST} !^your-site.com$ [NC] RewriteRule ^(.*)$ http://your-site.com/\ [L,R=301] |
source: WWW / No-WWW
hotlinkingの防止
「hotlinking」とはあなたのサイトの画像を勝手に他の誰かが利用することです。もちろん、あなたはhotlinkingの防止を望むでしょう。下記のスニペットは「mysite」を自身のURLに置き換えて利用してください。
1 2 3 4 5 6 | RewriteEngine On #Replace ?mysite\.com/ with your blog url RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC] RewriteCond %{HTTP_REFERER} !^$ #Replace /images/nohotlink.jpg with your "don't hotlink" image url RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L] |
feedをfeedbunnerにリダイレクト
WordPressなどのブログのfeedをfeedbunnerに向け直します。「yourfeed」など自身のものに置き換えて利用してください。
1 2 3 4 | <IfModule mod_alias.c> RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/ RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/ </IfModule> |
source: How to: redirect WordPress RSS feeds to feedburner with .htaccess
カスタムエラーページ
404などのエラーページに任意のファイルを指定します。エラーページとなるファイルをアップロードし、パスを自身のものに変更してください。
1 2 3 4 5 | ErrorDocument 400 /errors/badrequest.html ErrorDocument 401 /errors/authreqd.html ErrorDocument 403 /errors/forbid.html ErrorDocument 404 /errors/notfound.html ErrorDocument 500 /errors/serverr.html |
source: Custom Error Pages
ダウンロードファイルの処理
ユーザーがあなたのサイトからmp3, eps, xlsなどのファイルをダウンロードする際、ブラウザがどのようにするのかを指定します。
1 2 3 4 5 6 7 8 | <Files *.xls> ForceType application/octet-stream Header set Content-Disposition attachment </Files> <Files *.eps> ForceType application/octet-stream Header set Content-Disposition attachment </Files> |
source: Forcing a Download with Apache and .htaccess
PHPのエラーのログ
PHPファイルからログファイルへエラーを記録します。サーバーに「php_error.log」を作成し、7行目のパスを自身のものにして利用してください。
1 2 3 4 5 6 7 | # display no errs to user php_flag display_startup_errors off php_flag display_errors off php_flag html_errors off # log to file php_flag log_errors on php_value error_log /location/to/php_error.log |
source: PHP Error Logging
URLからファイルの拡張子を削除
ファイルの拡張子は開発者にとっては有用かもしれません。しかし、サイトのビジターにとっては絶対ではありません。下記の記述はHTMLファイルから「.html」を削除します。もちろんこれは「.php」など他の拡張子でも利用できます。
1 2 3 4 5 | RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html # Replace html with your file extension, eg: php, htm, asp |
source: Removing file extension via .htaccess
ディレクトリのファイルリストを見せない
サーバー上でインデックスファイルを持っていないディレクトリにアクセスすると、ファイルのリストを作成します。ビジターにファイルのリストを見せたくない時には、次のスニペットを使用してリスト化を制御してください。
1 | Options -Indexes |
ファイルを圧縮して軽量化
ウェブサイトに使用するさまざまなファイルを圧縮してページを軽量して、転送量を減らし、帯域を抑えます。
1 2 3 4 | AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch bMSIE !no-gzip !gzip-only-text/html |
文字コードの指定
.htaccessファイルで直接、指定した拡張子のファイルに特定の文字コードを指定します。
1 2 3 | <FilesMatch "\.(htm|html|css|js)$"> AddDefaultCharset UTF-8 </FilesMatch> |
source: Setting charset in htaccess
[ad#ad-2]
sponsors