0

Preface:

I am at following address:

http://www.example.com/#contact

After successful contact form submission, the user is redirected to following address:

http://www.example.com/thanks

With the help of following code:

// Redirect to thank you page
redirect_to("thanks");

The function redirect_to() is defined as:

// Page redirection
function redirect_to($url)
{
    if(isset($url) && $url != '')
    {
        header("Location: " . $url);
        exit();
    }
}

The htaccess rules for the thanks page are:

# Thanks Page
RewriteRule ^thanks/?$ thanks.php [QSA,L]
#RewriteRule ^thanks/$ thanks.php [QSA,L]

Problem:

The issue is the hash (#) character i.e. the fragment part is still sticking with the URL like:

http://www.example.com/thanks#contact

How does the part #contact get stick with main URL if I am redirecting the page to thanks?

How can we exclude/drop it either by htaccess or PHP or even through JS?

Sachin
  • 1,646
  • 3
  • 22
  • 59

1 Answers1

2

This question is asked before.

redirect is keeping hash

The response marked as the answer was:

The simple answer to "how do I stop it" is to specify an empty hash in the Location header:

header('Location: /account.html#');

However, this behavior isn't guaranteed across the board. It seems to work in WebKit and IE9 in my quick test. Nevertheless, you've stumbled on a black hole in the HTTP specification.

Tore
  • 51
  • 5
  • Firstly, nobody had asked it so clearly like I did. Isn't it? Secondly the solution you referred is not acceptable as I tried it and it didn't work at all. It added unnecessarily # at the end of url. Does that make any sense? – Sachin Sep 01 '17 at 06:02
  • In my opinion, it is just a workaround, not a good solution. But this is the solution I found, and at least in 2013, they claim it is a "black hole in the HTTP spec". Might be you'll have to live with it, unless someone has a more elegant solution – Tore Sep 01 '17 at 06:07
  • Actually, try the solutions here, one of them might help: https://stackoverflow.com/questions/16773111/removing-anchor-hash-from-url – Tore Sep 01 '17 at 06:10
  • OK, I will try it later. But no one is telling me how is it behaving like this? How does the fragment part suffix with the main URL? – Sachin Sep 01 '17 at 18:27