Archive

Archive

重複コンテンツを避ける.htaccess 記法

  • ホーム
  • Archive
  • 重複コンテンツを避ける.htaccess 記法
2025.12.22備忘録

「wwwなし」「index.htmlなし」「httpsのみ」の場合のリダイレクト

以下のコードを.htaccessに記述する。
「hoge.com」というドメインの部分は実際のドメインと差し替える

# URL書き換え有効化
Options +FollowSymLinks
RewriteEngine on

# wwwなしへリダイレクト
RewriteCond %{HTTP_HOST} ^www\.hoge\.com$
RewriteRule ^(.*)$ https://hoge.com/$1 [R=301,L] 

# index.htmlをリダイレクト
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ https://hoge.com/$1 [R=301,L]  

# SSLへリダイレクト
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

「wwwあり」「index.htmlなし」「httpsのみ」の場合のリダイレクト

以下のコードを.htaccessに記述する。
「hoge.com」というドメインの部分は実際のドメインと差し替える

# URL書き換え有効化
Options +FollowSymLinks
RewriteEngine on

# wwwありへリダイレクト
RewriteCond %{HTTP_HOST} ^hoge\.com$
RewriteRule ^(.*)$ https://www.hoge.com/$1 [R=301,L] 

# index.htmlをリダイレクト
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ https://www.hoge.com/$1 [R=301,L]  

# SSLへリダイレクト
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

エラーの場合以下を確認

.htaccess 記法の基本的なルール

ページ上部に戻るスプーン