Home > other >  Is a 301 redirect the same as changing window.location?
Is a 301 redirect the same as changing window.location?

Time:03-01

I want your answer, yes or no!

I am a web developer who also works in SEO. I want to 301 redirect one page to another page. Is this 301 redirect for Google beyond what I write below for you?

In JavaScript:

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

In PHP:

<?php 
    header("Location: https://example.com");
?>

Are these two 301 redirects, or do we have to write .htaccess in the cat file, for example?

CodePudding user response:

You can not do this with JavaScript.

But you can use PHP as follows

<?php 
    header("Location: https://example.com", TRUE, 301);
    exit;
?>

Syntax header

header(header, replace, http_response_code)

CodePudding user response:

Changing the URL with window.location in JavaScript is not a 301 redirect. JavaScript runs after the page has been generated on the server. Your JavaScript to change the URL would likely run on a page that has a 200 OK status.

That being said, Google treats JavaScript redirects very similarly to 301 permanent redirects. In most cases, Google will choose not to index the redirecting URL and pass the link juice from it to the target of the redirect.

On the other hand, clients that don't execute JavaScript won't see your JS powered redirect. That includes other search engines like Bing, Baidu, and Yandex, as well as broken link checkers and other SEO analysis tools.

Furthermore, even to Google, 301 redirects are a much stronger signal than a JavaScript redirect. Google is most likely to honor a redirect when it is a 301 Permanent variety compared to JS redirects, 302 Temporary redirects, or meta refreshes.

If you have the ability and opportunity to implement server side 301 Permanent redirects, you should do so instead of JS redirects.

  • Related