JavaScript Redirection – How to Redirect to Another Webpage for SEO?

JavaScript can be successfully used in SEO to create a redirection. Discover how to make the best use of JavaScript and create a redirection that will be respected by Google.

JavaScript SEO

Does Googlebot respect JavaScript redirects?

Yes. In the third episode of #AskGoogleWebmasters, John Mueller, a Google employee in the position of Webmaster Trends Analyst, explained that Google treats a JavaScript redirect in a similar way to a redirect made on the server side (e.g. 301 redirect).

The JavaScript redirect worked fine for my tests as well.

When to use JavaScript redirect?

You can use JavaScript redirection as needed, i.e. when, for example, for technical reasons, the 301 redirects cannot be performed on the server. By the way, 301 redirect is the most commonly used and recommended method of redirection, which has been supported by search engine robots for years. You can use HavaScript redirection also when appropriate code cannot be inserted in rel canonical.

Expert advice: For complex sites and specific situations, a JavaScript redirect can be useful to quickly index certain pages, e.g. all pages containing a keyword. The advantage of JavaScript redirection is that it is easy to implement. It is not necessary to play with the code executed on the server side or configuration files. Manually updating rel canonical or creating 301 redirects for hundreds of pages would be inefficient in terms of time and money.

How to use JavaScript redirect?

JavaScript implementation is very simple and can be done on any site. Just paste the code below into the head section of your website.

<script>
  window.location.replace("http://example.com/");
</script>

The above code will redirect you to the site http://example.com/

Expert advice: Don’t use JavaScript redirect with window.location.href, if you want to redirect the user to a new page immediately. This type of redirect adds the redirected page to your history and allows you to display the back button in the browser, which can cause loops when pressing the return button and bad user experience in a result.

How do I redirect all pages containing the selected keyword?

To do this, paste the code below into the head section of your website:

<script>
if ((document.documentElement.textContent || document.documentElement.innerText).indexOf('Exemplary brand name') > -1) {
  window.location.replace("http://example.com/");
}
</script>

The code was tested in an online store, where over 700 pages containing the keyword with the brand name had to be quickly indexed. Updating the source code on the store’s server-side in order to perform the appropriate 301 or rel canonical redirects was difficult in this case. The JavaScript redirection turned out to be great, the implementation process was fast.

Redirect JavaScript and Google Tag Manager

In extreme cases, if you cannot edit even the source code of the website, and you already have only the Google Tag Manager code installed, you can add JavaScript via Google Tag Manager (HTML tag).

However, I do not recommend this solution, because custom HTML tags are added to the end of the body tag (the page must partially load for the tag to be added). It may happen that the redirect is done a bit later than if it would be if it’s added in the head section of the site.

Have you also used JavaScript redirection in unusual situations, or do you prefer to use classic solutions (server-side 301 redirection or rel canonical)?

Rate this article

JavaScript Redirection – How to Redirect to Another Webpage for SEO? 0 (0)