サイトやブログの運営でよく使いそうな.htaccessの設定のまとめ

ウェブサイトやブログの運営でよく使いそうな便利な.htaccessの設定を紹介します。

こういうまとめは定期的にあがってきますが、やっぱり必要なのでシェアします。

サイトのキャプチャ

10 useful .htaccess snippets to have in your toolbox

[ad#ad-2]

下記は各ポイントを意訳したものです。

URLからwwwを削除

SEOなどの理由で、URLからwwwを削除して使うことがあるかもしれません。このスニペットは、あなたのウェブサイトにwww付きでアクセスしてきてもwww無しに向け直します。

RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]

source: WWW / No-WWW

hotlinkingの防止

「hotlinking」とはあなたのサイトの画像を勝手に他の誰かが利用することです。もちろん、あなたはhotlinkingの防止を望むでしょう。下記のスニペットは「mysite」を自身のURLに置き換えて利用してください。

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」など自身のものに置き換えて利用してください。

<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などのエラーページに任意のファイルを指定します。エラーページとなるファイルをアップロードし、パスを自身のものに変更してください。

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などのファイルをダウンロードする際、ブラウザがどのようにするのかを指定します。

<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行目のパスを自身のものにして利用してください。

# 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」など他の拡張子でも利用できます。

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

ディレクトリのファイルリストを見せない

サーバー上でインデックスファイルを持っていないディレクトリにアクセスすると、ファイルのリストを作成します。ビジターにファイルのリストを見せたくない時には、次のスニペットを使用してリスト化を制御してください。

Options -Indexes

ファイルを圧縮して軽量化

ウェブサイトに使用するさまざまなファイルを圧縮してページを軽量して、転送量を減らし、帯域を抑えます。

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ファイルで直接、指定した拡張子のファイルに特定の文字コードを指定します。

<FilesMatch "\.(htm|html|css|js)$">
AddDefaultCharset UTF-8
</FilesMatch>

source: Setting charset in htaccess

[ad#ad-2]

sponsors

top of page

©2024 coliss