Automatically redirect HTTP pages to their HTTPS equivalent. Smartly ignores local networks.
// ==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);
}
})();