HTTP to HTTPS Redirector

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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