htaccessコピペ集
.htaccess
書くときにいつもググってるなと思い、まとめてみました。
sample を対象のドメインに書き換えれば OK です。
Basic 認証
以下サイトを利用すると簡単に Basic 認証用.htaccess
を生成できます。
<Files ~ "^\.(htaccess|htpasswd)$"> deny from all </Files> AuthUserFile /home/sample/www/test/.htpasswd AuthGroupFile /dev/null AuthName "Please enter your ID and password" AuthType Basic require valid-user order deny,allow
www ありに統一
RewriteEngine on RewriteCond %{HTTP_HOST} ^sample\.com RewriteRule (.*) http://www.sample.com/$1 [R=301,L]
www なしに統一
RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.sample\.com RewriteRule (.*) http://sample.com/$1 [R=301,L]
http-→https に統一
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
index.html なしに統一
RewriteEngine on RewriteCond %{THE_REQUEST} ^.*/index.html RewriteRule ^(.*)index.html$ http://www.sample.com/$1 [R=301,L]
ファイル一覧非表示
ErrorDocument 404 http://sample.com/ ErrorDocument 403 http://sample.com/ DirectoryIndex index.html index.cgi index.php .ht
通信時に圧縮させるモジュール
<IfModule mod_deflate.c> SetOutputFilter DEFLATE BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|ico)$ no-gzip dont-vary Header append Vary User-Agent env=!dont-vary </IfModule>
ブラウザへのキャッシュの設定
<IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 30 minutes" ExpiresByType text/html "access plus 30 minutes" ExpiresByType text/css "access plus 1 days" ExpiresByType application/x-javascript "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType application/x-font-ttf "access plus 1 year" ExpiresByType application/x-font-woff "access plus 1 year" ExpiresByType application/x-font-opentype "access plus 1 year" ExpiresByType application/vnd.ms-fontobject "access plus 1 year" </IfModule>
旧ディレクトリのリダイレクト
about→concept へ
RewriteRule ^about/$ http://www.sample.com/concept/ [R=301,L]