✦ Helper Script v1.0 PRIVweb

⟳ redirect.js

Safe, conditional client‑side redirects — with validation & loop protection

A lightweight JavaScript helper that performs URL-based redirects after validating input, forcing HTTPS, and preventing infinite loops. Built by rayfounder (PRIVweb).

🔒 Validates Input 🔗 HTTPS Enforcement ⚡ Loop Prevention

1 ⟳ What This Script Does Overview

redirect.js is a self-contained JavaScript snippet that listens for a predefined redirect object on the page. When the current URL matches a specified pattern, it safely redirects the browser to a target URL — with added safeguards.

It is designed for static sites, single-page apps, or any environment where you need conditional, client‑side redirection without relying on server‑side configuration (e.g., .htaccess or nginx).

⚡ Core Features

📜 Zero Dependencies 🚀 ~1 KB Minified 🔑 Works Anywhere

2 ⚡ How It Works Step‑by‑Step

The script runs as an IIFE (Immediately Invoked Function Expression) and follows a strict validation pipeline. Here's the exact logic:

  1. Check for redirect object: If undefined, log a warning and exit.
  2. Validate redirect.from: Must be a non‑empty string with no spaces.
  3. Validate redirect.to: Must be a non‑empty string.
  4. Normalize target URL:
    • If target starts with / or ./, keep as relative.
    • Otherwise, force HTTPS: replace http:// with https://, or prepend https:// if missing.
  5. Prevent redirect loops: Compare current URL with the resolved target URL; if identical, abort silently.
  6. Check URL match: Convert current pathname + search + hash to lowercase and see if it contains redirect.from (lowercase).
  7. Redirect: If match found, perform window.location.replace(target) (no back‑button history).

💡 Real‑World Scenario

Suppose you have an old page /blog/old-post that you've moved to /blog/new-post. You can define redirect.from = "old-post" and redirect.to = "/blog/new-post". When a user visits the old URL, redirect.js will automatically send them to the new one.

3 🔗 Usage Example Copy‑Paste

To use redirect.js, define a redirect object before loading the script. Here's the basic setup:

<script>
  const redirect = {
    from: "example", // Can be changed to anything
    to: "https://example.com" // Can be changed to anything
  };
</script>
<script src="https://www.privweb.org/helper/redirect.js"></script>

How matching works: The script checks the current URL's pathname + search + hash — meaning it scans the entire URL after the domain, including all parameters, hash fragments, and subdirectories.

Case‑insensitive & real‑time: If you define from: "example", it will match any of these:

  • https://yoursite.com/example
  • https://yoursite.com/EXAMPLE
  • https://yoursite.com/page?q=example
  • https://yoursite.com/page?q=EXAMPLE
  • https://yoursite.com/page#example
  • https://yoursite.com/page#EXAMPLE
  • https://yoursite.com/example/page?utm=123
  • https://yoursite.com/page?ref=example&id=1
  • https://yoursite.com/any/path/example/anything
  • https://yoursite.com/ExAmPlE (any case variation)

In short — if the any part of your URL (path, query parameters, or hash) contains the string "example" (case‑insensitive), the redirect will fire.

💡 Pro tip: You can match #, =, &, /, and ? just by including them in the from string. the script treats them as plain text.

4 ⚠ Pros & Cons Balanced View

Like any tool, redirect.js has strengths and trade-offs. Here's an honest assessment.

❗ Advantages

  • Simple: No server‑side configuration needed.
  • Safe: Validates input, prevents loops, forces HTTPS.
  • Lightweight: ~1 KB minified, no dependencies.
  • Flexible: Matches any part of the URL (path, query, hash).
  • Case‑insensitive matching: User‑friendly.
  • Graceful degradation: Logs warnings if misconfigured.
  • No external calls: Works offline or behind firewalls.

⚠ Limitations

  • Client‑side only: Relies on JavaScript being enabled.
  • No 301/302 status: Search engines may not treat it as permanent.
  • Single rule per execution: You need to duplicate for multiple rules.
  • No wildcard/regex: Only substring matching (not pattern matching).
  • Not suitable for critical redirects: For SEO, server‑side is better.
  • No callback/events: Redirect is immediate, no hooks.

⚠ Important: For SEO-critical redirects (e.g., permanent moves), use server‑side 301 redirects. redirect.js is best for temporary or convenience redirection in client‑side applications.

5 ⚡ Integration Guide How to Use

▷ Step 1: Define the redirect object

In your HTML, create a <script> block and declare a variable named redirect with two properties:

You can use const, let, or var — the script only cares that a global redirect exists.

▷ Step 2: Load redirect.js

Include the script after the definition. The official CDN URL is:
https://www.privweb.org/helper/redirect.js
You can also host it locally if you prefer.

▷ Step 3: Deploy

Upload your HTML file to your web server. When a user visits a URL that contains the from string anywhere (path, query, or hash), they will be automatically redirected.

▷ Full Example

<!DOCTYPE html>
<html>
<head>
  <title>My Site</title>
  <script>
    const redirect = {
      from: "old-product",
      to:   "https://example.com/products/new-product"
    };
  </script>
  <script src="https://www.privweb.org/helper/redirect.js"></script>
</head>
<body>
  <!-- page content -->
</body>
</html>

▷ Notes on relative paths

If redirect.to starts with / or ./, it is treated as a relative path and not modified. Otherwise, it is assumed to be an absolute URL and will be forced to HTTPS.

The script compares the from string against location.pathname + location.search + location.hash. This means you can match query parameters (?key=value), hash fragments (#section), and even symbols like &, =, and / in real time.

6 🚀 Summary TL;DR

redirect.js is a pragmatic client‑side helper that adds a layer of safety and convenience to URL redirection. It shines in static sites, maintenance pages, or any scenario where you need quick, conditional redirects without touching server configs.

While it doesn't replace server‑side 301s for SEO, it's perfect for temporary campaigns, A/B testing, or simply cleaning up old links. Its validation, HTTPS enforcement, and loop prevention make it a reliable drop‑in solution.

"Redirect with confidence — no loops, no leaks, no hassle."