Are 301 redirects possible using javascript or jQuery?
Asked Answered
L

5

39

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

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

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.

Lowndes answered 12/12, 2012 at 19:0 Comment(0)
S
36

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.

Studhorse answered 12/12, 2012 at 19:2 Comment(0)
F
56

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).

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.

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

  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."

Filigreed answered 9/9, 2016 at 19:46 Comment(1)
Thank you for this answer, exactly what I was wondering when I found this question.Isopropanol
S
36

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.

Studhorse answered 12/12, 2012 at 19:2 Comment(0)
J
7

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

You'll have to do the 301 from PHP.

Jaquelynjaquenetta answered 12/12, 2012 at 19:3 Comment(0)
E
6

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

Educated answered 13/9, 2019 at 2:51 Comment(0)
U
2

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

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

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.

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

Unideaed answered 12/12, 2012 at 19:5 Comment(6)
@Queti: 301 redirects can be made from any server-side framework, not just PHP.Unideaed
I think you commented on the wrong answer. Anyways, I think Queti was just referring to PHP because that's the server tagged in the questionJerejereld
@AdrianBer But if you note, I put php in my tags so he referred me to my own scenario.Lowndes
Yeah, I missed the php tag, I just saw that you're using Apache, and you can do it with mod_rewrite in Apache, no PHP required.Unideaed
@AdrianBer Well, I want the redirect to be user-controlled so I don't think mod_rewrite is an option really.Lowndes
In this case, indeed is not. But be advised that browsers are caching 301 redirects.Unideaed

© 2022 - 2024 — McMap. All rights reserved.