How To 301 Redirect With Javascript?

Last Updated on


The quick answer is you cannot. You CAN redirect users with Javascript but it will not be a 301 redirect, or any other types of redirect for that matter.

The reason for that is the 301 HTTP status code is created at the server side and Javascript is executed at the browser level. In other words the Javascript cannot influence what is already executed by the server.

So what will happen is once the content is loaded to the browser, the Javascript will take effect and redirect users to a new content BUT, the page itself will remain status code 200 not 301.

This means search engines will get the same treatment and not seeing it as a 301 redirect.

However, a blog post published by RKG / Merkle documented that Google is now crawling Javascript pretty well and based on the experiment they carried out with Javascript based redirect, Google managed to interpret it as a 301 redirect.

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.

NOTE: Their experiment is based on a live page with status code 200 and NOT an inactive page. So if you want to implement this for legacy URLs that are no longer live, you might want to test it out first.

For most of us though, since our site is running on WordPress, we can simply use a plugin such as Redirection to set up a 301 redirect easily.


Few tools to help you generate 301 redirect code


0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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