Skip to main content

Web page 301 redirect scripts in HTML, JavaScript, PHP, ASP and ASP.net

- (incept: )

(X)HTML version

Uses a meta refresh URL redirect:

Language HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Redirecting</title>
  <meta http-equiv="refresh" content="0;url=http://websemantics.co.uk/">
</head>
<body>
</body>
</html>

The problem with this is that it isn't search engine friendly and the back button is broken.

Personally I'd add actual content to this page. Well at least a h1 heading and a paragraph explaining the page has moved and a link where to.

Language HTML
<h1>Page has moved</h1>
<p>
  It now resides here:
  <a href="http://websemantics.co.uk/">
    Page title
  </a>
</p>

Javascript version

In Javascript the best method is to use the location replace as it doesn't break the back button:

Language JavaScript
location.replace('http://websemantics.co.uk/');

Again the problem is that it isn't search engine friendly.

PHP version

The use of a 301 response in the header

Language PHP
<?php
  header( "HTTP/1.1 301 Moved Permanently" );
  header( "Location: http://websemantics.co.uk/" );
?>

ASP version

Language ASP
<%@ Language=VBScript %>
<%
  Response.Status = "301 Moved Permanently"
  Response.AddHeader "Location", "http://websemantics.co.uk/"
%>

ASPX (.net) version

Language ASPX
 <%@ Page Language = "C#" %>
 <script runat = "server">
    private void Page_Load ( object sender, System.EventArgs e ) {
      Response.Status = "301 Moved Permanently";
      Response.AddHeader("Location","http://websemantics.co.uk/");
    }
  </script>