首先必須正確安裝好憑證,沒問題後才執行下列動作,讓 WordPress 強制重定向到Https的網址,本篇提供兩種方法
.htaccess
在 WordPress 安裝目錄下的 .htaccess 檔案最前方加入下列幾行
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]純 WordPress PHP 方式
當主機不支援 .htaccess 或沒開啟 RewriteEngine 可試試看下面方法,用 PHP 方式重定向,結果完全一樣
<?php
/**
* v123 DEV
* https://v123.tw
*/
function v123_force_https () {
if ( !is_ssl() ) {
wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
exit();
}
}
add_action ( 'muplugins_loaded', 'v123_force_https', 1 );