HTTP to HTTPS Redirector

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

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
    }
})();