Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a http-equiv redirect that preserves query_string and fragment_id

In Javascript I can redirect and preserve the query string and fragment ID like this:

window.location = "NEW_LOCATION" + window.location.search + window.location.hash;

For sites without Javascript you can use the http-equiv meta header. But this drops the query string and fragment ID:

<head>
    <meta http-equiv="Refresh" content="300; url=NEW_LOCATION" />
</head>

Is there a way to do the equivalent using http-equiv="refresh" that preserves the query string and fragment ID?

like image 830
kanaka Avatar asked Feb 15 '12 21:02

kanaka


2 Answers

You could use JavaScript to update the meta tag with the query string and hash.

Update A better approach for IE8+ would be a noscript tag and a JavaScript powered redirect. Add the redirect as data-destination attribute on the html element so the script can grab it easily.

<!DOCTYPE html>
<html data-destination="http://stackoverflow.com">
  <head>
    <noscript><meta id="redirect" http-equiv="refresh" content="0; url=http://stackoverflow.com"></noscript>
  </head>

  <body>
    This page has moved. Redirecting...
    
  <!-- Redirect in JavaScript with meta refresh fallback above in noscript -->
  <script>
  var destination = document.documentElement.getAttribute('data-destination');
  window.location.href = destination + (window.location.search || '') + (window.location.hash || '');
  </script>
  </body>
</html>
like image 150
jwal Avatar answered Oct 04 '22 01:10

jwal


Not without a server-side scripting language which puts the proper url in the HTML tag (or sends a Refresh header directly).

like image 43
ThiefMaster Avatar answered Oct 04 '22 02:10

ThiefMaster