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.


STRAIGHTALK WEB SERVICE said,
March 20, 2007 @ 11:42 pm
I was just looking for a visual example of how a redirect was done, my hosting provides me with all these slick gadets as I call them, but i was tring to show my viewers a point about having webpages going to ‘404′ and how crucial but simple this matter can really be for any website Owner. Your article was of much help and if You had a rating I most certain to give this article an A+ ( 100 points)
Thanks and keep up the good work.
Webmaster at straightalk web services
Steven Hargrove said,
March 20, 2007 @ 7:28 am
Yes, a link to the new page without any redirection should suffice safely. Just be sure to not have the same content on two pages, meaning you will want to remove all of the old pages on your old domain to avoid a duplicate content filter in the search engines.
Daniele said,
March 20, 2007 @ 6:54 am
First of all, thanks for the quick answer… i didn’t count on it, with this being an old article and all.
And sadly, the situation is that. It’s a webspace that my client got with his ADSL line, so it’s crappy standard stuff… e-mail, third-level domain with 20Mb space, and NO server-side languages at all. More or less an FTP space… That is one of the reasons i’m moving the site to a better hosting (together with more Mbs required). The new location will have server-side stuff, databases, and all those nice gadgets… that i’d have needed in the old location to have a working redir
I’ve arrived here searching for redirect methods, but as i see server help is needed anyway… i think I’ll just have to go with the old-fashioned “the site has moved” page. A link is all a spider should need to find the new site from the old location, right?
Steven Hargrove said,
March 19, 2007 @ 4:14 pm
Daniele,
I am curious if the new server location you mention really does not have any server-side service to handle htaccess or some kind of server side scripting language. This seems odd, I would verify this and if it is true, I would see about installing htaccess capability onto this new server, it would be your best friend in some cases!
On the other hand, if what you say is true and you cannot have access to any of the methods above, the other option I would suggest in this case is yes, having a page mentioning that “this page has been moved” and have a link pointing to the new page, perhaps have thge link titled something to the effect of the topic of content of the new page. However more importantly, is having the link there and not using any sort of client-side auto-redirection, thats a no-no!
Daniele said,
March 19, 2007 @ 7:52 am
I was pondering to move the site of one of my clients from a ISP-related, third level domain to a second level to have more features, space etc. But now i’m stuck, since i read meta, javascript or Flash redirects are better not used… Then if someone has, like me, a plain FTP space, with just a name and no other language running, has to end up using just the classic page saying “Sorry, the site has moved to XYZ” ..? There is no other method?
Steven Hargrove said,
March 12, 2007 @ 10:25 am
Thanks for pointing that out, Sy. I have corrected it.
Sy said,
March 9, 2007 @ 4:51 pm
The ruby on rails example shouldn’t have the fancy quotes in it and should use regular double-quotes.
Ken Dyck said,
March 4, 2007 @ 9:33 am
Thanks for the tip, Steve. Works like a charm.
Jim B said,
March 3, 2007 @ 4:26 pm
Hello Steve,
Excellent information!
I have a different situation that I need to forward to an eStore that does not have meta tags for search engines. I was recomended to use a redirect with meta tags and description. I was going to do this in html but as you suggested that is a no no.
using index.php or htaccess, is it possible to write the redirect with meta tags and description of web site. Can you please help and advise on a sample code.
Thank you,
Jim B.
Bobbie Dennis said,
March 2, 2007 @ 12:57 pm
Hi there,
I am writing to you as a non-techie webmaster of a local government website. We use javascript for our redirects, which are used partly for departments to have a user-friendly URL to give out, e.g., http://cityofchesapeake.net/parks. Using the javascript rather than meta refresh enabled users to use the BACK button to get back to the homepage. However, looking for a better solution, I came across your site and this page about 301 redirects. We do not use ASP, PHP, JAVA, etc., but do have Apache.
I was interested in trying the .htaccess, but have never worked with a .htacess file before. I have a couple of questions:
1) In creating the page, do you have any other coding such as your tags or anything like that? If so, where do you put the 301 redirect coding?
2) When you create the .htaccess file do you give ia name such as parks.htaccess, or is it just .htaccess?
3) Can you put only one line of code for one redirect, or could you have several 301 redirects listed in this one .htaccess page?
As you can see I am a novice, although I have evolved enough with 10 years of working on our site to keep it going!
Steve, thanks so much for any help that you have time to provide.
Bobbie Dennis
Webmaster
City of Chesapeake, VA
(757) 382-8419