HTTP to HTTPS Redirector

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
    }
})();