http转Https已经成为了现在网站的一个趋势,https可以说代表着更安全、更放心的一种协议。那么http转为https,就成为网站建设的一个标准了。那么wordpress网站中http跳转https的方法有哪些呢?

一、服务器配置

1)、nginx配置,以rewrite开头的一行就是配置,意思是把网站路径重写为https协议的网站路径。

server {
listen       80;
server_name  localhost;
rewrite ^(.*)$ https://$host$1 permanent;
location / {
root   html;
}
或者301跳转的形式
server {
    listen 80;
    
    server_name www.xxx.com;
    
    return 301 https://$host$request_uri;
}

2)、apache服务器在配置文件中或者根目录 .htaccess 文件加入如下代码

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]

3)、IIS配置

如果网站根目录有Web.config文件则在web.config中配置

<system.webServer>
  <rewrite>
   <rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
     <match url="(.*)" />
     <conditions>
      <add input="{HTTPS}" pattern="off" ignoreCase="true" />
     </conditions>
     <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
   </rules>
  </rewrite>
 </system.webServer>

根据IIS版本配置

IIS6.0 路径:C:WINDOWS->Helpiis->Helpcommon->403-4.htm IIS7.0以上 路径:C:inetpub->custerr->zh-CN->403.htm 把以下代码替换(403-4或403)里面所有内容,然后保存

<HTML><HEAD><TITLE>该页必须通过安全通道查看</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=GB2312">
</HEAD><BODY>

<script type="text/javascript">
var url = window.location.href;
    if (url.indexOf("https") < 0) {
     url = url.replace("http:", "https:");
     window.location.replace(url);
    }
</script>
</BODY></HTML>

二、第三方工具的使用,该方式具有简单便捷的优点

1)、phpstudy-》网站-》管理-》修改-》其它-》http跳转https

2)、宝塔的301重定向,登录宝塔-》网站-》设置-》重定向-》将http跳转到https

三、网站级配置

1)、在wordpress根目录下的index.php中define( 'WP_USE_THEMES', true );的上面一行添加如下代码

$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])&& $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 1 : 0;
if(!$http_type){
    echo '<script>window.location="https://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'";</script>';exit;
}

       2)、在主题的functions.php中加入下面的代码