HTML Redirect

From Web Developer Network Wiki
Jump to: navigation, search

How to redirect users to a different page.


If you want to redirect from a page that is still there, or is linked to from many external sources, here's some code that will redirect them. Whenever possible, use HTTP redirects. HTTP redirects tells the browser the new location of the file it is looking for by sending a [HTTP status code (3xx)]. Files that are no longer there should be removed or coded to send a HTTP status code 301 (moved permanently).

  • If your server runs Apache and your server administrator has enabled [.htaccess], create a .htaccess file at the root directory with a line:
Redirect /oldpath http://server/newpath

For example, if we would like to make a redirect from http://www.unl.edu/webdevnet/abc.html to http://www.unl.edu/webdevnet/xyz.html, we would create a .htaccess at our directory root with the following line:

Redirect /webdevnet/abc.html http://www.unl.edu/webdevnet/xyz.html


  • If your server has PHP enabled, you may use the PHP header() function to make redirects.

For example, if we would like to make a redirect from http://www.unl.edu/webdevnet/abc.php to http://www.unl.edu/webdevnet/xyz.php, we would replace the contents of abc.php with the following:

<?php
header("Location: http://www.unl.edu/webdevnet/xyz.php");

exit;
?>


  • If none of those listed above are available, use this workaround.

<html>
<head>
<script type="text/javascript">
<!--
location.replace("http://www.unl.edu/");
//-->
</script>
<noscript>
<meta http-equiv="refresh" content="0; URL=http://www.unl.edu/">
</noscript>
</head>
<body>
</body>
</html>

Just replace 'http://www.unl.edu/' with the new URL.


Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox