(各種版本) http怎么做自動跳轉(zhuǎn)https?
- 威海網(wǎng)站建設(shè) 威海https
- 2700
IIS7以上版本
1. 安裝rewrite組件
2. 找到網(wǎng)站根目錄web.config文件,替換一下內(nèi)容(如果沒有此文件可以創(chuàng)建一個);
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<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>
</configuration>
復(fù)制代碼
3.重啟IIS測試訪問。
APache 版本
如果需要整站跳轉(zhuǎn),則在網(wǎng)站的配置文件的<Directory>標(biāo)簽內(nèi),鍵入以下內(nèi)容:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R=301]
復(fù)制代碼
如果對某個目錄做https強(qiáng)制跳轉(zhuǎn),則復(fù)制以下代碼:
RewriteEngine on
RewriteBase /yourfolder
RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
復(fù)制代碼
如果只需要對某個網(wǎng)頁進(jìn)行https跳轉(zhuǎn),可以使用redirect 301來做跳轉(zhuǎn)!
redirect 301 /你的網(wǎng)頁 https://你的主機(jī)+網(wǎng)頁
Nginx版本
在配置80端口的文件里面,寫入以下內(nèi)容即可。
server {
listen 80;
server_name localhost;
rewrite ^(.*)$ https://$host$1 permanent;
location / {
root html;
index index.html index.htm;
}
復(fù)制代碼
單獨(dú)頁面通用代碼段:以下方法較適合指定某一個子頁單獨(dú)https
在需要強(qiáng)制為https的頁面上加入以下代碼進(jìn)行處理http-->https
<script language="JavaScript" type="text/JavaScript">
function redirect()
{
var loc = location.href.split(':');
if(loc[0]=='http')
{
location.href='https:'+loc[1];
}
}
onload=redirect
</script>
復(fù)制代碼
在需要強(qiáng)制為http的頁面上加入以下代碼進(jìn)行處理
https-->http
<script language="JavaScript" type="text/JavaScript">
function redirect()
{
var loc = location.href.split(':');
if(loc[0]=='https')
{
location.href='http:'+loc[1];
}
}
onload=redirect
</script>
復(fù)制代碼
PHP頁面跳轉(zhuǎn):添加在網(wǎng)站php頁面內(nèi)
if ($_SERVER["HTTPS"] <> "on")
{
$xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
header("Location: ".$xredir);
}
復(fù)制代碼
http跳轉(zhuǎn)https的方法較多,以上僅供參考。(本文引用沃通)
來源:景安