teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2017/08/17 07:35

投稿

退会済みユーザー
answer CHANGED
@@ -1,4 +1,20 @@
1
1
  これで動きませんか?
2
2
  ```ここに言語を入力
3
3
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
4
- ```
4
+ ```
5
+
6
+ **追記**
7
+ 別に.htaccessでなくても良いのならプラグインなど使わなくてもis_sslという関数でHTTPかHTTPSか判別してリダイレクトが可能です。
8
+ [https://codex.wordpress.org/Function_Reference/is_ssl
9
+ ](https://codex.wordpress.org/Function_Reference/is_ssl)
10
+ テーマのfunctions.phpに以下のように書いておけば良いだけです。
11
+
12
+ ```
13
+ function force_https_redirect() {
14
+ if ( !is_ssl() ) {
15
+ wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
16
+ }
17
+ }
18
+ add_action ( 'template_redirect', 'force_https_redirect', 1 );
19
+ ```
20
+