違う案件でもコピペで使い回しOKな「.htaccess」でのURL正規表現(リダイレクト)
.htaccessでのhttpからhttpsへのリダイレクト・wwwありなしのリダイレクト・index.htmlありなしのリダイレクトをサイト毎に書くのが面倒なので、ドメイン関係なくコピペで使いまわせる.htaccessの書き方。
スポンサーリンク
https・wwwなし・index.htmlなしの場合
<IfModule mod_rewrite.c>
RewriteEngine On
# https以外からhttpsにリダイレクト
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# wwwありからwwwなしにリダイレクト
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# index.htmlありからindex.htmlなしにリダイレクト
RewriteCond %{REQUEST_URI} ^.*/index\.html$
RewriteRule ^(.*)index\.html$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
WordPressサイトの場合は、これを「# BEGIN WordPress」の行より前に書く。
スポンサーリンク