6

For some reason when I redirect with header("Location") the new page keeps the hash.

So if you're on example.com/index.html#signup

I redirect with

header("Location: /account.html");
exit;

But then it shows example.com/account.html#signup

Why is this happening and how can I stop it? ie example.com/account.html


Note:

I am using an .htaccess to redirect file.html to file.php

RewriteRule ^([a-zA-Z0-9-_.]+)\.html$ $1.php [L]
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124

2 Answers2

5

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.

Community
  • 1
  • 1
N Rohler
  • 4,595
  • 24
  • 20
1

I'm looking around for answer that works on Firefox. After a while surfing here and there while my cat is keeping meow meow, it leads me to the final solution:

die('<script> window.location='your-url-without-hash';</script>');

Sometimes you want to reserve the hash, sometimes you don't, and because the cross-browser matters so it's better to control client browser to reload and kill the hash.

Brian Ng
  • 1,005
  • 12
  • 13