HTTP to HTTPS Redirector

Automatically redirect HTTP pages to their HTTPS equivalent. Smartly ignores local networks.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         HTTP to HTTPS Redirector
// @namespace    SFRUUCB0byBIVFRQUyBSZWRpcmVjdG9y
// @version      1.1
// @description  Automatically redirect HTTP pages to their HTTPS equivalent. Smartly ignores local networks.
// @author       smed79
// @license      GPLv3
// @icon         https://i25.servimg.com/u/f25/11/94/21/24/https11.png
// @match        http://*/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 1. Double-check we are actually on HTTP
    if (window.location.protocol !== 'http:') {
        return;
    }

    // 2. Ignore local networks, routers, and localhost environments
    // This prevents breaking access to 192.168.x.x routers and local development servers
    const host = window.location.hostname;
    const isLocalNetwork = /^(localhost|127\.0\.0\.1|192\.168\.\d+\.\d+|10\.\d+\.\d+\.\d+|172\.(1[6-9]|2[0-9]|3[0-1])\.\d+\.\d+|\[::1\])$/.test(host);
    
    if (isLocalNetwork) {
        return;
    }

    // 3. Construct the HTTPS URL safely
    // Substring(5) strips the "http:" part perfectly every time
    const httpsUrl = "https:" + window.location.href.substring(5);

    // 4. Redirect using replace() so we don't break the browser's "Back" button
    try {
        window.location.replace(httpsUrl);
    } catch (error) {
        console.error('Failed to redirect to HTTPS:', error);
    }
})();