Skip to the indexed answers
Digital Marketing Index

usdigitalmarketingindex.com SEO

Are JavaScript redirects suitable replacements for permanent server-side redirects?

No, not when a server-side redirect is possible. A server-side permanent redirect answers the request itself with a status code and the new address, so every client learns of the move at once without loading or running anything. A JavaScript redirect only takes effect after the page has been fetched, rendered, and its script executed. Google can process JavaScript redirects, and its documentation lists them among the permanent redirect types, with a plain instruction attached: “Only use JavaScript redirects if you can’t do server-side or meta refresh” redirects (Google’s guidance on redirects). Use them as a last resort on platforms that give you no access to the server’s responses.

Different redirect mechanisms

Google’s documentation lists six permanent methods: HTTP 301, HTTP 308, a meta refresh of 0 seconds, an HTTP refresh of 0 seconds, JavaScript location, and what it calls a crypto redirect, which it says not to rely on. The three compared below differ in where the instruction lives.

Method Where the redirect is stated When a client learns of it
HTTP 301 or 308 In the response status and Location header On the first response, before any content
Meta refresh, 0 seconds In the HTML head After the HTML is received and parsed
JavaScript location In a script After the page is rendered and the script runs

A server-side redirect, as a client sees it:

$ curl -sI https://www.example.com/old-page
HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/new-page

A JavaScript redirect, as the same request sees it:

$ curl -sI https://www.example.com/old-page
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8

The status is 200. Nothing in the response says the page has moved. The move is in the body:

<script>
  window.location.replace('https://www.example.com/new-page');
</script>

Crawl and rendering dependencies

That difference has consequences.

  • Rendering is required. Google’s JavaScript documentation describes rendering as a separate, later phase, in which a page “may stay on this queue for a few seconds, but it can take longer than that” (Google’s JavaScript SEO basics). The redirect is discovered only then.
  • The script must run. If it is blocked by robots.txt, fails, or depends on something unavailable, the redirect does not happen, and the old page is treated as a normal 200 page.
  • Other clients may never see it. Crawlers and tools that do not execute scripts receive a 200 and the old content. Google’s documentation notes that “not all bots can run JavaScript”.
  • The old URL returns a success code. Link checkers, monitoring, and caches all read it as a live page.
  • Visitors see the old page load before being sent on.

None of this means a JavaScript redirect is ignored by Google. It means the instruction arrives later and through more steps, each of which can fail.

Correct server implementation

For a permanent move, return a 301 or 308 from the server, straight to the final URL. Google’s documentation advises: “Use permanent redirects when you’re sure that the redirect won’t be reverted”.

Examples in two common server configurations. The paths are illustrative.

# nginx
location = /old-page {
    return 301 https://www.example.com/new-page;
}
# Apache, in the site configuration or .htaccess
Redirect 301 /old-page https://www.example.com/new-page

Where the server cannot be configured, check the alternatives in order.

  1. The hosting platform’s or CDN’s redirect rules, where it provides them.
  2. The CMS’s redirect feature or a redirect plugin, which issue server-side responses.
  3. A meta refresh with a delay of 0 seconds, in the head of the old page.
  4. A JavaScript redirect, if nothing above is available.

Exceptions and validation

Legitimate uses of a JavaScript redirect:

  • A hosted platform that allows editing page content and nothing else.
  • A single-page application sending an unknown route to a URL that returns a real 404. Google’s JavaScript documentation describes that as one of its two strategies for avoiding soft 404 errors.
  • Routing that depends on something only the browser knows, after the page has loaded.

If you must use one, reduce the fragility.

  • Place the script in the head, inline, so it runs early and does not depend on an external file.
  • Use location.replace so the old URL is not left in the browser’s history.
  • Redirect to the final URL, with no further hops.
  • Add a canonical tag on the old page pointing to the new URL, and a visible link to the new page in the body for clients that run no script.
  • Do not block the page or its scripts in robots.txt.

Validation for either method:

Check Server-side redirect JavaScript redirect
Header request to the old URL 301 or 308 with the new Location 200; the redirect is not visible here
Browser Lands on the new URL Lands on the new URL after the old page begins to load
Fetch without scripts Follows to the new URL Stays on the old page
URL Inspection on the old URL Reported as a page with redirect Run a live test and read the rendered result
URL Inspection on the new URL Indexable, canonical to itself Same
Internal links and sitemap Updated to the new URL Same
Over the following weeks The new URL replaces the old in the performance report Same, and watch for the old URL lingering

The third row is the practical test of how much depends on scripts.

Whichever method is used, update internal links to point at the new address directly, so that the site itself does not rely on the redirect. And avoid stacking redirects. What happens when redirects are chained one after another is covered in the guide to how multi-hop redirects affect search crawler efficiency.

The answer in one line: a permanent move belongs in the server’s response. A script is what you use when the server is out of reach.

Link trail

Reader comments on this answer

Add your comment to this answer

Your email address will not be published. Required fields are marked *