How to redirect a web page, the smart way
The internet today is full of webmasters that are always updating, editing and even deleting web pages.
Lets say you are updating your website completely, changing the names of page's filenames (ex: file.html to file.php) and so on, this is great, you should stay updated! But what if you want to get rid of those old pages without having to worry about those who go to the old web page and see nothing? It doesnt end there either, other visitors do include major search engines such as MSN, Google and Yahoo! If people are finding your old pages when querying in these search engines, and they attempt to go to that page that has been deleted or moved, they will get a "404 File Not Found" Error! Now i know you dont want that, no webmaster wants that!
UPDATE: For those of you still confused on what web page redirection is, I have written a follow-up article titled Understanding Web Page Redirection, the smart way, to help answer some of the questions I most commonly get in the comments of this article.
The 301 Redirect
The best way to redirect those pages is by using something called a "301 Redirect". What this 301 redirect does, is it blatantly redirects to a different page when it is triggered, what makes the 301 redirect the best, is that not only does it accomplish your redirect, it does it safely, no having to worry about the search engines penalizing you for it! To be specific, the 301 redirect tells the browser, or in other cases, it tells the search engines "Hey this page has been moved, here is the correct URL!". Think of it as you getting mail that is not addressed to your name, possibly addressed to somebody who has lived there prior to yourself, what do you do? You tell the post man (or woman) "Hey they dont live here anymore, here is the correct address". It is the same concept guys, pretty simple if you asked me!
So lets get started. Below you will see several methods of using the 301 redirect, including the redirect in PHP, the redirect in ASP, the redirect in ASP .NET, the redirect in JSP (JAVA), the redirect in IIS, the redirect in ColdFusion, the redirect in CGI/PERL and finally the one I find most useful, the redirect using htaccess. Also showing other useful ways of using the 301 redirect with mod_rewrite!
HTML Redirection
How do you redirect using html you ask? Here is how: DONT!
Over the past 4-6 years, use of meta tag refresh redirection has been abused for uses in relation to SPAM. The result of this and other scenarios of mis-uses of it, is that when using it, that page WILL be de-indexed from every search engine.
NOTE: This also applies to javascript redirection. Search engines can easily detect javascript and meta tag redirection, so just dont do it, use the 301 redirect.
301 Redirect Using htaccess
Using htaccess to accomplish the 301 redirect is highly suggested due to it being fairly convenient to manage, rather than setting redirects on each individual page, you can simply add the redirect code to the .htaccess file.
Here is how to do it:
- Create a file on the root directory of your website, name it ".htaccess".
- Open the .htaccess file using notepad or what ever text editor that you prefer.
- Add this into the .htaccess file, save it and then upload it to your web server:
CODE:
-
Redirect 301 /old/old.html http://www.you.com/new.html
-
NOTE: Don't add "http://www" to the first part of the statement - place the path from the top level of your site to the page. Also ensure that you leave a single space between these elements:
redirect 301 (the instruction that the page has moved) /old/old.html (the original folder path and file name) http://www.you.com/new.html (new path and file name)
Also note that you are not required to redirect the page to another domain, an equally useful purpose for using the 301 redirect, is redirecting old pages to the new pages on the same domain, it all works the same way!
UPDATE: .htaccess Editor is a simple, yet useful resource for generating htaccess files.
301 Redirect Using Mod_Rewrite
Mod_Rewrite has got to be one of the most usefull modules a server can have in terms of SEO, it allows to organize the file structure of your web site in a dynamic yet simple fashion, in this example I show a useful method of 301 redirecting with mod_rewrite.
When somebody links to your website, sometimes they dont always link to you in the way that you want them to. If somebody links to www.yoursite.com and somebody else links to yoursite.com, Google will assign a separate pagerank for each of those. Yes, it is stupid but it is true, by inserting the below example into your .htaccess file, it will solve the problem by redirecting anything linking to yoursite.com to www.yoursite.com, also redirecting the pagerank, so no worries!
-
RewriteEngine On
-
rewritecond %{http_host} ^yoursite.com
-
rewriteRule ^(.*) http://www.yoursite.com/$1 [R=301,L]
301 Redirect Using IIS
- In internet services manager, right click on the file or folder you wish to redirect.
- Select the radio titled "a redirection to a URL".
- Enter the page that the page will be redirected to.
- Check "The exact url entered above" and the "A permanent redirection for this resource".
- Click on 'Apply'.
301 Redirect Using ColdFusion
As well as many server side scripting languages, using the 301 redirect in them is fairly simple.
Simply add this code to your ColdFusion page:
-
<cfheader statuscode="301" statustext="Moved permanently">
-
<cfheader name="Location" value="http://www.new-url.com/">
301 Redirect Using PHP
Simply add this code to your page or script:
301 Redirect Using ASP
Simply add this code to your page or script:
-
<%@ Language=VBScript %>
-
<%
-
Response.Status="301 Moved Permanently"
-
Response.AddHeader "Location", "http://www.new-url.com/"
-
%>
301 Redirect Using ASP .NET
Simply add this code to your page or script:
-
<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>
301 Redirect Using JSP/JAVA
Simply add this code to your page or script:
-
<%
-
response.setStatus(301);
-
response.setHeader( "Location", "http://www.new-url.com/" );
-
response.setHeader( "Connection", "close" );
-
%>
301 Redirect Using CGI/PERL
Simply add this code to your cgi/perl script:
-
$q = new CGI;
301 Redirect Using Ruby/Ruby on Rails
(Thanks to Codeninja) Simply add this code to your ruby/ruby on rails script:
-
def old_action
-
headers["Status"] = "301 Moved Permanently"
-
redirect_to "http://www.mynewpageorsite.com/"
-
end
Pleaee note that all of the snippets of code above are examples and I have tested each at some point. However, I am in no way responsible for any damage the code may cause, you use this code at your own risk.


Jimbo said,
June 18, 2006 @ 11:59 am
Or if you haven’t the time you could use a free service like
GoStubby.com they offer true 301 redirects, and many other features!
I Agree 301 is the best way to go, very search engine friendly!
301 is permnent, S.E.O friendly
302 temporary, and you do get penelized
Eric Newman said,
May 26, 2006 @ 1:45 pm
hi steven i know this isnt about web design but how do you make a picture as a icon because i save a pic to a .ico for an icon file so when i pick the picture to change it says it isnt any icons and it says it is an icon file.
please help
Steven Hargrove said,
May 24, 2006 @ 12:41 pm
invoking a 404 error will stop the client in it’s tracks, whether it be me visitng the page, or a spider crawling the website, basically if there was once content on the page and it has been moved to a new location, it is always a good idea to redirect the old url to the new. I highly suggest noy invoking a 404 on your pages if simply the url’s have been changed.
James said,
May 24, 2006 @ 9:12 am
Thz for your speedy reply Steven.
Currently I using this method by inputing this line in .htaccess:-
ErrorDocument 404 http://mysite.com
would it cause any foreseenable harm? In term of SEO? robots crawling? google PR?
Thanks again for your kind guides.
Steven Hargrove said,
May 24, 2006 @ 2:11 am
well, I believe I understand what you mean…
there are a few things you could do actually, the site appears to be created with php, so you could place some php logic somewhere in the coding to check if that particular forum thread or section exists, etc. However outside of this there isnt a way to detect a 404 error prior to it actually giving a 404
Another way you may be able to do it is by using mod_rewrite to redirect all page named similarly, for example something like this::
This would redirect any page with the category[X].html?[x] to your home page, but then again you couldnt really rule out all of the other pages with it so it may not be very helpfull, although you might want to check some official mod_rewrite documentation to get a better understanding of it.
James said,
May 21, 2006 @ 5:53 pm
Thz for this guide.
I need some help:-
I just revamp my old site to a forum, thus all my old pages are having error as “Not Found” of course!
How do I re-direct all those old pages to my home page irregardless of the old path as long as it is 404error?
eg: http://suggestionsite.com/category24.html?PHPSESSID=2347ab8edbc321d3a54ab9b1f9aed45c
http://suggestionsite.com/category128.html?PHPSESSID=24c7a27cee7a03c4193ddf7ef6289630
http://suggestionsite.com/category21.html?PHPSESSID=2347ab8edbc321d3a54ab9b1f9aed45c
AT the moment, I’m manually redirect it one by one, and it is just very painful in doing it one by one. Any advise to re-direct alll the old pages not found at one go?
Sincerely
James
Eric Newman said,
May 19, 2006 @ 2:08 pm
what do you mean “redirect you are trying to use, and what kind of language? php? asp?”Im just trying to make a music web page using web page maker and i put a sourse in after i use the web program to put radio.blog on and when i do that, i load the page up and it shows what i wrote last to you it stills shows the page but it has a little window that has all that writting i wish i can explain more but i can’t
Steven Hargrove said,
May 19, 2006 @ 1:43 pm
Eric, I understand that you are having a problem,but I am still waiting for you to tell me what kind of redirect you are tyring to use, and what kind of language? php? asp? what?
William, redirecting a web page with flash is highly not recommended as it is a quick way to get penalized by a search engine because it does violate the guidelines of the major search engines. Most sites that I have seen that use flash to redirect a page, they seem to assume that search engines wont notice, but trust me that could not be further from the truth.
William said,
May 18, 2006 @ 9:51 pm
I recently visited a web page that used a Flash redirect. Have any idea how this can be done?
Eric Newman said,
May 18, 2006 @ 7:54 pm
This is what it says on my page
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
——————————————————————————–
Invalid at the top level of the document. Error processing resource ‘file:///C:/Documents and Settings/Eric/Desktop/Music F…
“; ?>
————————————————–^