How To 301 Redirect With Javascript?
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.
References
http://www.somacon.com/p145.php
https://moz.com/community/q/301-redirect-using-javascript
Few tools to help you generate 301 redirect code
http://www.rapidtables.com/web/tools/redirect-generator.htm
http://www.contentforest.com/seo-tools/url-redirect-generator
- How To Track Keyword Ranking? - April 27, 2020
- Is Updating Old Content For SEO Worth It? - March 30, 2020
- What Is Remote Desktop Access (a.k.a RDP Or VPS) For SEO? - March 27, 2020
The post reveals some burning issues and questions that ought to be discussed and explained.
Moreover, it is vital to grasp within the very detail.
From the article, an individual can easily find something basic,
unusually for him, something which could be exceedingly helpful.
I am thrilled with the data I have just got. Thank you a lot!
I suggest you write such sorts of posts every day to give the audience like me all the
necessary info. In my opinion, it’s far better to be
ready for all the unexpected scenarios in advance, so thanks, it was fairly cool.
You did a fantastic job! This post seem very great.
This doesn’t factor in that javascript is full stack. When running client side, javascript isn’t going to process a redirect as a 301, but when running as middleware for a server side application or backend, javascript CAN handle 301 redirects. This article assumes all javascript is front end only and barely scratches the surface, further terrifying SEOs and marketers when working with developers. Stick to wordpress.