Mike Q
Mike Q

Reputation: 7327

reload page with different URI anchor

This is a simplified PHP version of my question:

I want to load http://localhost/test2.php in browser, and if it's missing #test_test, reload immediately with it http://localhost/test2.php#test_test .

  <?php

  // causes ERR_TOO_MANY_REDIRECTS
  if (strpos(strtolower($_SERVER['REQUEST_URI']), 'test_test') === false) {
      header("Location: /test2.php#test_test");
  }

  ?>     

In short, I want to check for an anchor in an URI, if it's missing, reload the page with it. I am getting ERR_TOO_MANY_REDIRECTS. Any suggestions on how to tackle this?

Upvotes: 0

Views: 74

Answers (1)

Sergei Klykov
Sergei Klykov

Reputation: 1037

You can do it using js this way:

let currentUrl = window.location.href;

if (currentUrl.indexOf('#test_test') === -1)  {
    window.location.href = currentUrl.replace(/(#.+)/gi, '#_test_test');
}

Upvotes: 1

Related Questions