From time to time you’ll probably want to send a website visitor who tries to access a page that has moved, been deleted, etc to a brand spanking new page of which quite possibly will have the content they were looking for.

In this article I’ll introduce you to the 301 re-direct, show you how to create one, and you’ll see why sometimes just depending on the 404 page isn’t practical.

What is a 301 re-direct?

The exact term is the “301 Permanent Redirect”, and basically it means re-directing someone who visits your website and tries to access a page that has moved, or you just need to re-direct them elsewhere for whatever reason. This is when the 301 re-direction comes into play.

The number ‘301′ is simply the error code used in HTML so the web browser understands what’s going on, or what is about to go on.

Using a 301 re-direct (or redirect) is also a great way to either make sure that users enter your website using the www in the URL or enter without the www. This is called the 301 Canonical Redirect and is often used for SEO purposes.

Method 1: The .htaccess file (Apache)

The .htaccess file should be found in your website’s root directory on the web server. If you’re unsure about this, contact your server administrator.

The Apache way to re-direct a user trying to access one page and get em’ on over to another page is below..

1
Redirect 301 /old/oldpage.htm /new/http://www.domain.com/newpage.htm

To change domains you could use the following code..

1
2
3
4
RewriteEngine On
 
RewriteCond %{HTTP_HOST} ^.*oldwebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.preferredwebsite.net/$1 [R=301,L]

Apache .htaccess Canonical Redirect

1
2
3
4
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

Method 2: Using PHP

Paste the following code in whichever associated .PHP file would be most logical. Some will want to use the header file, others the index file, and some might even use the code in a blog posting.

1
2
3
4
5
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/page.html");
exit();
?>

PHP Canonical Redirect

1
2
3
4
5
6
7
<?php
if (substr($_SERVER['HTTP_HOST'],0,3) != ‘www’) {
header(’HTTP/1.1 301 Moved Permanently’);
header(’Location: http://www.’.$_SERVER['HTTP_HOST']
.$_SERVER['REQUEST_URI']);
}
?>

Method 3: Using ASP

1
2
3
4
<%
Response.Status="301 Moved Permanently"
Response.AddHeader='Location','http://www.new-url.com/'
%>

ASP Canonical Redirect

The Canonical Redirect with ASP must be located in a script that is executed in every page on the server before the page content starts.

1
2
3
4
5
6
7
8
<%
If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www."
& Request.ServerVariables("HTTP_HOST")
& Request.ServerVariables("SCRIPT_NAME")
End if
%>

Method 4: Using HTML

Let’s face it, we don’t all use the fancy scripting methods within our websites.

The below code would of course go into your header file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/
Content-Type: text/html
Content-Length: 174
 
<html>
<head>
<title>Moved</title>
</head>
<body>
<h1>Moved</h1>
<p>This page has moved to <a href="http://www.example.org/">http://www.example.org/</a>.</p>
</body>
</html>

Well, that’s it! I hope you guys find this tutorial useful, and as always, a link back to the article or a comment is much appreciated.

Related posts on JungleJar:

Tagged with:
Categorized as: articlesbloggingdevelopmentphpseotutorialswebdevelopment


8 Brilliant Insights!

  • Jim Gaudet says:

    You just can’t get enough 301 info…

  • admin says:

    haha, well, I can’t imagine ever really needing much more than this.

  • Wayne says:

    I liked that you covered several languages as examples. quite helpful to see it put all together like that. Most people will show it in the language they know well and ignore the others. Good stuff, thanks for sharing it.

  • admin says:

    And thank you Wayne for leaving a comment. Much appreciated.

  • Ashley says:

    Hi,

    I am slightly confused over all the 301 coding.

    I purchased an old site recently and I have built a new site in a dev.mydomain.com for exmaple… I have managed to list in a spread sheet all the redirect pages that I want redirecting to the new correct individual page of which there are some 647 in total (so not a small job!), but they all are highly ranked on google so I have to do this correct.

    However, I was told the other day that all I need to do is create one .htaccess file showing each individual code change, which is fine but I just can’t find out how to do this????

    I use dreamweaver, so do I just once I do find out how, just save the file as .htaccess in dreamweaver? I am a self taught web site runner since 94 but never really had this before, never needed it as always planned sites perfectly.

    Your guidance would be brilliant?

  • Ashley says:

    Actually solved it now, easy… feel a fool!

  • Alex45 says:

    Reload this zone file using the steps outlined in the earlier section: Reloading the Zone. ,

Have valuable insight?


CommentLuv Enabled
preload preload preload