.htaccessでURLの正規化をする方法です。
設定するのは以下3つ
- httpsに強制
 - wwwをありに統一、または、なしに統一
 - index.html(htm/php)をなしに統一
 
目次
https強制、wwwあり、indexなし
<Ifmodule mod_rewrite.c>
  RewriteEngine On
  # httpsに統一
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  # wwwありに統一
  RewriteCond %{HTTP_HOST} !^www\.(.*) [NC]
  RewriteRule ^ https://www.%{HTTP_HOST}/$1 [R=301,L]
  # indexなしに統一
  RewriteCond %{THE_REQUEST} ^.*/index.(html|htm|php)
  RewriteRule ^(.*)index.(html|htm|php)$ https://%{HTTP_HOST}/$1 [R=301,L] 
</Ifmodule>https強制、wwwなし、indexなし
<Ifmodule mod_rewrite.c>
  RewriteEngine On
  # httpsに統一
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  # wwwなしに統一
  RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
  RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
  # indexなしに統一
  RewriteCond %{THE_REQUEST} ^.*/index.(html|htm|php)
  RewriteRule ^(.*)index.(html|htm|php)$ https://%{HTTP_HOST}/$1 [R=301,L]
</Ifmodule>ドメイン名をべったり書いている例とか見かけますが、個人的にはあまり好きじゃないので上記のようにしています。
あと、RewriteEngine On を何回も書いている.htaccessを見かけることもありますが、1回でいいです。<IfModule mod_rewrite.c>はWordPressとか入れていると既に記述があったりしますので、環境に合わせてください。
アイキャッチ画像背景素材:著作者:Hello-Pixel/出典:Freepik
