.htaccessファイルでURLをhttps、wwwありに統一する

よく利用するけど、よく忘れるのでメモです。


# ------------------------------------------------------------------------------
# // URL統一
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
  RewriteEngine on

  # -- http→https
  RewriteCond %{HTTPS} off
  RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  # -- wwwあり
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  # -- wwwなし
  # RewriteCond %{HTTP_HOST} ^www\.
  # RewriteRule .* https://(wwwなしホスト名)%{REQUEST_URI} [R=301,L]

</IfModule>

※2017年9月6日更新
wwwなし もコメント部分に追記。