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).
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).
from string (case‑insensitive).redirect.from and redirect.to are non‑empty strings, and that from has no spaces.http:// targets to https:// and adds https:// if no protocol is given (except for relative paths).The script runs as an IIFE (Immediately Invoked Function Expression) and follows a strict validation pipeline. Here's the exact logic:
redirect object: If undefined, log a warning and exit.redirect.from: Must be a non‑empty string with no spaces.redirect.to: Must be a non‑empty string./ or ./, keep as relative.http:// with https://, or prepend https:// if missing.pathname + search + hash to lowercase and see if it contains redirect.from (lowercase).window.location.replace(target) (no back‑button history).
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.
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/examplehttps://yoursite.com/EXAMPLEhttps://yoursite.com/page?q=examplehttps://yoursite.com/page?q=EXAMPLEhttps://yoursite.com/page#examplehttps://yoursite.com/page#EXAMPLEhttps://yoursite.com/example/page?utm=123https://yoursite.com/page?ref=example&id=1https://yoursite.com/any/path/example/anythinghttps://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.
Like any tool, redirect.js has strengths and trade-offs. Here's an honest assessment.
⚠ 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.
redirect object
In your HTML, create a <script> block and declare a variable named
redirect with two properties:
from – a string (no spaces) to match against the current URL. The match is case‑insensitive and checks the entire pathname + search + hash.to – a string destination (can be relative, absolute, or external).You can use const, let, or var — the script only cares that a global redirect exists.
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.
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.
<!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>
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.
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."