How to Redirect a Web Page

HTTP response status codes 301 and 302 are used to indicate the browser that the requested page (URL) is to be found at another location. The 301 Moved Permanently status code tells the browser just that : the web page has been moved to another location permanently. This also tells search engine to remove to old URL from their index and to replace it with the new one.

The 302 Found status code tells the browser that the document has been found but redirects the browser to another location to get its content. In this case, search engines may have different behaviors. Perhaps you may want to read this article from Matt Cutt on the subject.

In this tutorial, I will show you multiple ways of creating a redirect.

Redirect Using .htaccess

Assuming your website is hosted on a Apache web server :

301 Redirect:

redirect 301   /oldpage.html    http://www.new-url.com/newpage.html

302 Redirect:

redirect       /oldpage.html    http://www.new-url.com/newpage.html

Redirect Using PHP

301  Redirect:

Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.new-url.com/newpage.html” );
?>

302 Redirect:

header(”Location: http://www.new-url.com/newpage.html”);
exit();
?>

Redirect Using ASP.net

301 Redirect:

<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(“Location”,”http://www.new-url.com”);
}
</script>

302 Redirect:

<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “302 Found”;
Response.AddHeader(“Location”,”http://www.new-url.com”);
}
</script>

Redirect Using Ruby on Rails

301 Redirect:

def old_action
headers["Status"] = “301 Moved Permanently”
redirect_to “http://www.new-url.com/”
end

302 Redirect:

def old_action
headers["Status"] = “302 Found”
redirect_to “http://www.new-url.com/”
end

Of course there are even other ways of performing redirection, for instance using IIS but that’s for another post.

Stay tuned!

0 responses so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment