Panduan JavaScript Redirects

What sebuah JavaScript redirect adalah, how Google's render pipeline treats ini differently dari sebuah server-side 301, when ini adalah sebuah acceptable last resort, dan cara implement dan detect one — plus where meta refresh dan History API fit.

Pertama kali diterbitkan: 27 Jun 2026 · Terakhir diperbarui: 3 Agu 2026 · Advanced
Bahasa
1 sinyal bukti di halaman ini

sebuah JavaScript redirect mengirim pengguna dan crawler untuk sebuah baru URL dengan client-side code (window.location.replace() atau.href). ini adalah paling sedikit reliable redirect jenis because Google hanya sees ini setelah rendering — which dapat menjadi delayed, atau fail entirely, dengan no fixed timeline either cara. Google's official order dari preference adalah server-side (301/302/307/308) → meta refresh → JavaScript, dan -nya docs say plainly: hanya gunakan JS redirects jika Anda dapat't melakukan lainnya two. Once Google successfully interprets one, target becomes sebuah canonicalization signal — tetapi itu's not sebuah proven guarantee dari identical PageRank atau peringkat outcomes untuk sebuah 301, so treat ini sebagai sebuah last resort alih-alih sebuah like-untuk-like swap. gunakan them pada constrained platforms dengan no server access, untuk SPA halaman error itu poin untuk sebuah nyata 404, dan little else. jika Anda harus gunakan one, gunakan window.location.replace() di <head>, drop source URL dari Anda sitemap, dan poin tautan internal di akhir destination. Meta refresh adalah sebuah separate HTML-tingkat redirect, dan history.pushState()/replaceState() aren't redirects di semua.

TL;DR — sebuah JavaScript redirect adalah sebuah client-side redirect (window.location.replace(), .href, .assign()) itu Google hanya processes setelah rendering — phase three dari crawl → render → indeks. sebuah server-side 301 adalah seen di crawl time; sebuah JS redirect waits pada render queue, dan Google gives no fixed timeline untuk itu wait — ini dapat menjadi quick atau ini dapat take sebuah panjang while, dan rendering dapat fail outright. Google’s documented preference order adalah server-side → meta refresh → JavaScript, dan docs say untuk gunakan JS redirects hanya when Anda dapat’t melakukan lainnya two. Once Google successfully interprets one, target becomes sebuah permanent canonicalization signal — bahkan Google digunakan them pada mereka own blog when nothing else worked — tetapi itu’s not documented proof dari identical PageRank atau peringkat outcomes untuk sebuah 301, so mereka’re sebuah last resort, not sebuah spam signal. legitimate menggunakan adalah constrained platforms dengan no server config dan SPA halaman error itu poin di sebuah nyata 404. Implement dengan window.location.replace() di <head>, drop source dari Anda sitemap, repoint tautan internal, dan confirm Googlebot dapat fetch JS. Meta refresh adalah HTML-tingkat (0s = permanent, apa pun delay = temporary), dan history.pushState()/replaceState() aren’t redirects di semua.

What counts sebagai sebuah JavaScript redirect

Script navigation depends pada rendering dan execution, so ini adalah not protocol-equivalent untuk sebuah HTTP redirect. Evidence for this claim Primary standard or official documentation supporting the adjacent article claim. Scope: Protocol semantics and Search behavior are kept separate; no indexing, ranking, or migration-timing guarantee is inferred. Confidence: high · Verified: Google: JavaScript redirects Processing adalah mungkin, not sebuah exact-timing atau pengindeksan guarantee. Evidence for this claim Primary standard or official documentation supporting the adjacent article claim. Scope: Protocol semantics and Search behavior are kept separate; no indexing, ranking, or migration-timing guarantee is inferred. Confidence: high · Verified: Google: Redirects and Search

sebuah JavaScript redirect navigates browser untuk sebuah baru URL dengan client-side code. umum metode, dan how mereka differ:

  • window.location.replace("url") — navigates dan menghapus original URL dari session history. ini adalah one untuk gunakan: back button skips redirect source alih-alih bouncing pengguna straight back.
  • window.location.href = "url" — navigates tetapi mempertahankan original di history, so back button mengembalikan untuk redirecting halaman (dan dapat buat loop). document.location.href dan window.location.assign("url") behave sama cara.
  • history.pushState() / history.replaceState()not redirects. mereka rewrite address bar without apa pun navigation atau HTTP signal, so crawler don’t treat them sebagai redirects. SPAs gunakan them untuk di-app URL perubahan; mereka perlu nyata <a href> tautan (atau actual navigations) untuk menjadi dapat di-crawl.

dividing line itu penting: .replace(), .href, dan .assign() semua trigger sebuah nyata document navigation ( difference antara them adalah hanya what happens untuk session history), while pushState()/replaceState() tidak pernah navigate di semua — mereka touch history state dan address bar dan nothing else. None dari four adalah sebuah HTTP 301; “redirect” (terjemahan) “redirect” here adalah shorthand untuk client-side navigation, not sebuah status code.

Meta refresh (<meta http-equiv="refresh" content="0;url=...">) adalah sering lumped di dengan JS redirects, tetapi ini adalah sebuah HTML directive parsed sebelum JavaScript runs — sebuah separate, more reliable category, covered below.

How Google processes sebuah JavaScript redirect

ini adalah crux. Google’s pipeline runs di stages, dan sebuah JS redirect dan sebuah server-side redirect get caught di berbeda ones:

  1. crawl — Googlebot fetches URL dan reads raw HTML. sebuah server-side 301/302/307/308 adalah seen right here.
  2. Render queue — halaman itu kembalikan 200 wait untuk menjadi rendered. Google’s docs note halaman “may stay on this queue for a few seconds, but it can take longer than that.” (terjemahan) “dapat stay pada ini queue untuk sebuah few seconds, tetapi ini dapat take longer daripada itu.” Google doesn’t publish sebuah fixed service-tingkat timeline beyond itu, so treat wait sebagai unpredictable — ini dapat menjadi quick, atau ini dapat drag — rather daripada assuming apa pun spesifik angka dari days atau weeks.
  3. Render + indeks — headless Chromium runs JavaScript. ini adalah pertama moment sebuah JS redirect exists sebagai far sebagai Google adalah concerned.

yang sama idea I membuat pada crawling dan di JavaScript SEO applies here: rendering adalah sebuah separate langkah dari fetching, dan anything itu depends pada ini inherits itu delay dan itu risk.

dan risk adalah nyata. Google: “While Google attempts to render every URL Googlebot crawled, rendering may fail for various reasons. This means that if you set a JavaScript redirect, Google might never see it if rendering of the content failed.” (terjemahan) “While Google attempts untuk render setiap URL Googlebot di-crawl, rendering dapat fail untuk various alasan. ini berarti itu jika Anda set sebuah JavaScript redirect, Google mungkin tidak pernah see ini jika rendering dari konten failed.” (Google Search Central) selama window sebelum redirect adalah processed — dan forever, jika rendering fails — Google dapat pertahankan empty source halaman di -nya indeks.

Google’s official preference order

redirects documentation lays out sebuah hierarchy dari sebagian besar untuk least reliable:

  1. server-side redirects — 301/308 (permanent), 302/307 (temporary). Best untuk everything: seen di crawl time, unambiguous.
  2. Meta refresh — HTML-tingkat. sebuah 0-kedua meta refresh adalah treated sebagai sebuah permanent redirect (like sebuah 301); apa pun delayed meta refresh adalah treated sebagai temporary.
  3. JavaScript redirects — last resort.

Google’s kata: “Only use JavaScript redirects if you can’t do server-side or meta refresh redirects.” (terjemahan) “hanya gunakan JavaScript redirects jika Anda dapat’t melakukan server-side atau meta refresh redirects.” Permanent redirects pass canonical signal untuk target; temporary ones pertahankan original di hasil. (How itu interacts dengan canonical selection adalah di atas pada Canonicalization.)

melakukan tautan equity pass melalui?

Google’s own redirects documentation lists JavaScript location navigation among -nya permanent redirection metode, dan says target becomes canonicalization signal once Google memiliki interpreted ini — so flat “JS redirects don’t pass PageRank” (terjemahan) “JS redirects don’t pass PageRank” claim adalah salah. What documentation doesn’t establish adalah itu outcome adalah identical, immediate, atau sebagai reliable sebagai sebuah server-side 301 — ini describes canonical signal, not sebuah guarantee dari matching PageRank flow, peringkat, atau timing. honest framing: sebuah 301 passes signal di crawl time dengan near-certainty; sebuah JS redirect passes ini hanya jika dan when rendering succeeds, dan Google doesn’t promise hasil akan match sebuah 301’s outcome one-untuk-one. itu gap — not lost equity — adalah nyata cost dari choosing JavaScript.

ini adalah juga why JS redirects aren’t sebuah penalty trigger pada mereka own. mereka hanya become sebuah spam masalah when mereka’re digunakan untuk cloaking — showing crawler one halaman dan redirecting pengguna untuk something berbeda, atau sending mobile pengguna untuk sebuah unrelated domain. Google’s sneaky redirects policy adalah tentang itu intent, not technique.

When sebuah JavaScript redirect adalah right alat

ada legitimate cases:

  • Constrained platforms. beberapa shared hosting, CDN, atau CMS setups give Anda no access untuk server-side redirect aturan. sebuah JS redirect adalah sebuah valid fallback — dan notably, Google digunakan JS redirects pada mereka own Webmaster blog because, sebagai Gary Illyes put ini, “that was the only thing we could use for 1:1 redirects, and it works on Google” (terjemahan) “itu adalah satu-satunya thing kami dapat gunakan untuk 1:1 redirects, dan ini berfungsi pada Google” (OnCrawl).
  • SPA error handling. Google explicitly endorses ini: “Use a JavaScript redirect to a URL for which the server responds with a 404 HTTP status code.” (terjemahan) “gunakan JavaScript redirect untuk sebuah URL untuk which server responds dengan sebuah undefined HTTP kode status.” (Google Search Central) sebuah single-halaman app itu resolves sebuah buruk route dapat redirect untuk sebuah nyata 404 endpoint so Google processes error correctly alih-alih pengindeksan sebuah soft 404.

untuk permanent URL migrations, ini adalah not alat — gunakan 301. I membuat yang sama poin di situs migrations: JavaScript redirects adalah sebuah last resort, dan Google dapat tidak pernah see them.

Static situs generators: Hugo aliases trap

sebuah umum surprise: Hugo’s aliases: frontmatter memiliki historically generated meta refresh HTML halaman, not server-side 301s — dan lainnya static generators memiliki done similar things oleh default. Generator defaults perubahan antara versi, so periksa Anda saat ini deployed versi’s actual output alih-alih assuming; jika aliases: isn’t giving Anda 301s, Anda perlu platform-tingkat redirect aturan (Netlify _redirects, Cloudflare Workers, Vercel vercel.json) plus (pada Hugo) disableAliases: true. I cover ini di detail di Hugo SEO.

Implementation best practices

jika sebuah JavaScript redirect adalah genuinely Anda hanya option:

  • gunakan window.location.replace(), not .href. sebagai mesin pencari Journal puts ini, JS redirects “typically use window.location.replace() function rather than window.location.href to avoid UX redirect loops” (terjemahan) “typically gunakan undefined function alih-alih undefined untuk hindari UX redirect loops” (SEJ).
  • Put ini di <head>, not <body>. browser parse HTML sequentially dan run scripts sebagai mereka hit them, so “position JavaScript redirects in the <head> tag rather than <body> to minimize delay” (terjemahan) “position JavaScript redirects di undefined tag alih-alih undefined untuk minimize delay” (OnCrawl).
  • Redirect untuk akhir destination di one hop. sebuah JS redirect untuk sebuah halaman itu itself 301s elsewhere membuat sebuah chain; chains waste anggaran crawling dan dapat surface di GSC sebagai sebuah redirect error.
  • hapus source URL dari Anda sitemap XML. Sitemaps seharusnya list canonical, dapat diindeks URLs — not redirecting ones.
  • Repoint tautan internal di destination, so mereka don’t route melalui redirect di semua.
  • pastikan Googlebot dapat fetch JS. jika redirect lives di sebuah external script blocked oleh robots.txt, Google dapat’t render ini dan won’t see redirect.

cara detect JavaScript redirects

mereka don’t announce themselves like sebuah 301 di sebuah header, so Anda memiliki untuk render:

  • sebuah crawler dengan JS rendering pada. OnCrawl recommends crawling dengan “JavaScript rendering enabled (5-second timeout minimum)” (terjemahan) “JavaScript rendering enabled (5-kedua timeout minimum)”; Screaming Frog dan Ahrefs situs Audit dapat both render. Without rendering, sebuah JS-redirecting halaman hanya looks like sebuah normal 200.
  • Chrome DevTools. Network tab (dengan “Preserve log” (terjemahan) “pertahankan log”) menampilkan client-side navigation; Redirect Path extension flags ini too.
  • di Search Console, sebuah successfully processed JS redirect menampilkan up di bawah halaman dengan redirect — yang sama status sebagai apa pun redirected URL, which adalah normal untuk non-canonical sources. itu label isn’t guaranteed pada apa pun given periksa, though: ini reflects whatever Google fetched, rendered, interpreted, dan canonicalized di sampled moment, so sebuah URL dapat tampilkan sebuah berbeda status (atau no redirect status yet) antara memeriksa without itu menjadi sebuah error pada Anda end.

What I’d actually melakukan

server-side pertama, setiap time. Meta refresh (0-kedua) when Anda dapat edit HTML tetapi not server config. JavaScript hanya when both adalah off table — dan lalu dengan window.location.replace() di <head>, sebuah clean sitemap, dan sebuah periksa bahwa redirect actually renders untuk Googlebot. untuk anything permanent atau tinggi-nilai, extra reliability dari sebuah 301 adalah worth almost apa pun effort untuk obtain.

Add an expert note

Pin an expert quote

New person? Create their unclaimed profile at /admin/experts/ → Pin a quote first.