
(自分も含め)何度も検索されている方へ・・・
SEO・ 表示スピード対策に、最低限必要(だと思われる)なhtaccessの記述をまとめました。以下の機能が利用可能です。
※ページ下部で、Githubからダウンロードできます。
記述内容
- index.html/phpなしに統一
- httpsに統一
- 指定のディレクトリを対象から外す
- 404リダイレクト
- 各種リダイレクト
- キャッシュコントロール
記述
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
AddType application/x-httpd-php .php .html RewriteEngine on # === RewriteRuleセット ================================ #↓ドキュメントルートを設定する RewriteBase /xxx/ # --- index.html/phpなしに統一 --- RewriteCond %{THE_REQUEST} ^.*/index.html #↓指定のディレクトリを対象外にする。ポイントは、RewriteCondとRewriteRuleの間に毎回書くこと。 #RewriteCond %{REQUEST_URI} !(zzz/) RewriteRule ^(.*)index.html$ $1 [R=301,L] RewriteCond %{THE_REQUEST} ^.*/index.php #↓指定のディレクトリを対象外にする。ポイントは、RewriteCondとRewriteRuleの間に毎回書くこと。 #RewriteCond %{REQUEST_URI} !(zzz/) RewriteRule ^(.*)index.php$ $1 [R=301,L] # --- httpsに統一 --- RewriteCond %{SERVER_PORT} 80 #↓指定のディレクトリを対象外にする。ポイントは、RewriteCondとRewriteRuleの間に毎回書くこと。 #RewriteCond %{REQUEST_URI} !(zzz/) RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] # === 各種リダイレクト ================================ #↓リダイレクト関連はドキュメントルートで記述 # --- 404リダイレクト --- ErrorDocument 404 /xxx/http404/ ErrorDocument 403 /xxx/http404/ # --- 各種リダイレクト --- Redirect permanent /xxx/sample/ /xxx/sample2 # === キャッシュコントロール ================================ #Thanks:https://html5boilerplate.com/ <IfModule mod_expires.c> ExpiresActive on ExpiresDefault "access plus 1 month" # CSS ExpiresByType text/css "access plus 1 year" # Data interchange ExpiresByType application/json "access plus 0 seconds" ExpiresByType application/xml "access plus 0 seconds" ExpiresByType text/xml "access plus 0 seconds" # Favicon (cannot be renamed!) ExpiresByType image/x-icon "access plus 1 week" # HTML components (HTCs) ExpiresByType text/x-component "access plus 1 month" # HTML ExpiresByType text/html "access plus 0 seconds" # JavaScript ExpiresByType application/javascript "access plus 1 year" # Manifest files ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds" ExpiresByType text/cache-manifest "access plus 0 seconds" # Media ExpiresByType audio/ogg "access plus 1 month" ExpiresByType image/gif "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType video/mp4 "access plus 1 month" ExpiresByType video/ogg "access plus 1 month" ExpiresByType video/webm "access plus 1 month" # Web feeds ExpiresByType application/atom+xml "access plus 1 hour" ExpiresByType application/rss+xml "access plus 1 hour" # Web fonts ExpiresByType application/font-woff "access plus 1 month" ExpiresByType application/vnd.ms-fontobject "access plus 1 month" ExpiresByType application/x-font-ttf "access plus 1 month" ExpiresByType font/opentype "access plus 1 month" ExpiresByType image/svg+xml "access plus 1 month" </IfModule> |
サーバーによって動かないときもあるけど焦らない。
大概は動作するとは思いますが、htaccessはサーバーによって動かないときもあります。そんなときは、「該当の記述 + サーバー名 + 動かない」などで検索する解決するでしょう。
他に、追記してほしい項目があれば、Githubでイシューをもらえるとうれしいです。
コメントを残す