httpとhttps、wwwありとwwwなしの統一
apache .htaccess リダイレクト 効かない場合
htaccess設定
.htaccessに下記を記述。
場合によっては、「Options +SymLinksIfOwnerMatch」または「Options +FollowSymLinks」を追加する。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
wwwなしで統一する場合は下記
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
RewriteCond %{HTTPS} offがうまく動かないとき
WordPressを利用している場合、「wp-config.php」に下記を入力する。
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') $_SERVER['HTTPS']='on';
それでもリダイレクトがうまく動かないとき
WordPressを利用している場合、「functions.php」に下記を記入する
function force_https_redirect() {
if ( !is_ssl() ) {
wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
}
}
add_action ( 'template_redirect', 'force_https_redirect', 1 );