是否可以使用 javascript 或 jQuery 进行 301 重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13846738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:56:58  来源:igfitidea点击:

Are 301 redirects possible using javascript or jQuery?

javascriptjqueryseohttp-status-code-301

提问by Caleb Doucet

I'm running Apache 2.0 and I'm just wondering if it is possible to make a 301 redirect using JavaScript or jQuery.

我正在运行 Apache 2.0,我只是想知道是否可以使用 JavaScript 或 jQuery 进行 301 重定向。

I have an <a></a>tag with hrefto a specified location and I'm asked to make a 301 redirect when I click that link.

我有一个指向指定位置的<a></a>标签,href当我单击该链接时,系统要求我进行 301 重定向。

This is for SEO and I'm trying to find a way to do the 301 redirect to the same page in the link without having to create a new page or create a form/submit.

这是针对 SEO 的,我正在尝试找到一种方法来将 301 重定向到链接中的同一页面,而无需创建新页面或创建表单/提交。

采纳答案by techfoobar

In short No.

总之没有

JavaScript runs entirely on the client side. 301 redirects are supposed to come as a response from the server. Which means you cannot do this without server support.

JavaScript 完全在客户端运行。301 重定向应该是来自服务器的响应。这意味着如果没有服务器支持,您将无法执行此操作。

回答by Chris Searles

I know this is an old question but the answers don't really address the primary issue that was presented in the question which is a 301 redirect for SEO purposes (and the answer today may very well be different than it was when the question was originally asked and answered).

我知道这是一个老问题,但答案并没有真正解决问题中提出的主要问题,即用于 SEO 目的的 301 重定向(今天的答案很可能与问题最初出现时的不同问并回答)。

The answer that no, you can't 301 redirect from the client is technically correct, however (and more importantly) you don't necessarily need to. While a true 301 would be preferred, in cases like this one where it's not possible (or transitioning away from hashbang URLs back to traditional URLs for example), the question is really whether or not there's a viable alternative that accomplishes the goal.

不,您不能从客户端进行 301 重定向的答案在技术上是正确的,但是(更重要的是)您不一定需要这样做。虽然真正的 301 是首选,但在这种不可能的情况下(例如,从 hashbang URL 转换回传统 URL),问题实际上是是否有可行的替代方案来实现目标。

Search Engine Land did a detailed testof Google's capabilities regarding JavaScript and this is the related excerpt from that article:

Search Engine Land对 Google 在 JavaScript 方面的能力进行了详细测试,这是该文章的相关摘录:

  1. JavaScript Redirects

We first tested common JavaScript redirects, varying how the URL was represented in different ways. The method we chose was the window.location function. Two tests were performed: Test A included the absolute URL attributed in the window.location function. Test B used a relative URL.

Result: The redirects were quickly followed by Google. From an indexing standpoint, they were interpreted as 301s — the end-state URLs replaced the redirected URLs in Google's index.

In a subsequent test, we utilized an authoritative page and implemented a JavaScript redirect to a new page on the site with exactly the same content. The original URL ranked on the first page of Google for popular queries.

Result: As expected, the redirect was followed by Google and the original page dropped from the index. The new URL was indexed and immediately ranked in the same position for the same queries. This surprised us, and seems to indicate that JavaScript redirects can (at times) behave exactly like permanent 301 redirects from a ranking standpoint.

The next time your client wants to implement JavaScript redirects for their site move, your answer might not need to be, “please don't.” It appears there is a transfer of ranking signals in this relationship. Supporting this finding is a quote from Google's guidelines:

"Using JavaScript to redirect users can be a legitimate practice. For example, if you redirect users to an internal page once they're logged in, you can use JavaScript to do so. When examining JavaScript or other redirect methods to ensure your site adheres to our guidelines, consider the intent. Keep in mind that 301 redirects are best when moving your site, but you could use a JavaScript redirect for this purpose if you don't have access to your website's server."

  1. JavaScript 重定向

我们首先测试了常见的 JavaScript 重定向,以不同的方式改变 URL 的表示方式。我们选择的方法是 window.location 函数。执行了两个测试: 测试 A 包括了 window.location 函数中的绝对 URL。测试 B 使用了相对 URL。

结果:重定向很快就被谷歌跟踪了。从索引的角度来看,它们被解释为 301——最终状态的 URL 替换了谷歌索引中的重定向 URL。

在随后的测试中,我们使用了一个权威页面,并实现了一个 JavaScript 重定向到网站上具有完全相同内容的新页面。原始网址在热门查询的 Google 第一页上排名。

结果:正如预期的那样,重定向被 Google 跟踪,原始页面从索引中删除。新 URL 已编入索引,并立即在相同查询的相同位置中排名。这让我们感到惊讶,并且似乎表明 JavaScript 重定向(有时)从排名的角度来看与永久 301 重定向完全一样。

下次您的客户想要为其网站移动实施 JavaScript 重定向时,您的回答可能不必是“请不要”。似乎在这种关系中存在排名信号的转移。支持这一发现的是谷歌指南的引述:

“使用 JavaScript 重定向用户可能是一种合法的做法。例如,如果您在用户登录后将用户重定向到内部页面,则可以使用 JavaScript 来这样做。在检查 JavaScript 或其他重定向方法以确保您的网站符合要求时根据我们的指南,考虑意图。请记住,在移动您的网站时最好使用 301 重定向,但如果您无权访问您网站的服务器,则可以使用 JavaScript 重定向来实现此目的。”

回答by Andrés Nava - Azure

301 is a server response code. You would not be able to create a 301 redirect from jQuery.

301 是服务器响应代码。您将无法从 jQuery 创建 301 重定向。

You'll have to do the 301 from PHP.

您必须从 PHP 执行 301。

回答by Adrian Ber

301 redirectsare permanent redirects and are basically HTTP server responses. JavaScript/jQuery is something that is executed on the client. Two different worlds.

301 重定向是永久重定向,基本上是 HTTP 服务器响应。JavaScript/jQuery 是在客户端执行的东西。两个不同的世界。

Instead you can actually put in href the final URL if you cannot do it on the server.

相反,如果您无法在服务器上执行此操作,您实际上可以将最终 URL 放入 href 中。

Or, if you're asking if you can redirect the current page, yes it's possible with META redirects or by changing the window.location.

或者,如果您问是否可以重定向当前页面,是的,可以使用 META 重定向或更改 window.location。

If you're using Apache you can use mod_rewriteto do a 301 redirect.

如果您使用的是 Apache,您可以使用mod_rewrite进行 301 重定向。

回答by Abhishek

If this is for SEO purpose only then this will work
<meta http-equiv="refresh" content="0;url=YOUR_URL">
Google considers this as a 301 redirect though it's not

如果这仅用于 SEO 目的,那么这将起作用
<meta http-equiv="refresh" content="0;url=YOUR_URL">
Google 将其视为 301 重定向,尽管它不是