佳文收藏 - How to redirect a web page, the smart way
本文引自:http://www.stevenhargrove.com/redirect-web-pages/
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? 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 don't live here anymore, here is the correct address". It is the same concept, pretty simple stuff.
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 8-10 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.
Now, this doesn't necessarily apply to *everything*. If it is used properly, then you have nothing to worry about. For example, often on many forum engines - you will see when you perform different tasks such as making a thread, replying to one or even things like logging in - you will notice it takes you to a "please wait a moment" screen. On those types of pages, they are using either javascript or meta tag redirection, and its perfectly legit.
Canonical Links
My first impression of canonical links is "Great, but where the hell were you 10 years ago?". That aside, from what I see they really are for one purpose: dynamic content.
To put that into perspective, I have a wordpress plugin on this blog that will separate my comments onto multiple pages, so I don't have to show 500+ comments on one single page. So it paginates the comments for me.
The slight drawback to this, is that since it is paginating my comments onto several pages - this exact article data exists on those pages as well. So those page url's end up looking like this: http://www.stevenhargrove.com/redirect-web-pages/comment-page-50/. When really, it is just the same page with different comments.
Using canonical links, I could specify in the of the html document my canonical link. Which would look like this:
HTML:
<link rel="canonical" href="http://www.stevenhargrove.com/redirect-web-pages/" />
Redirect 301 /old/old.html http://www.you.com/new.html
RewriteEngine Onrewritecond %{http_host} ^yoursite.comrewriteRule ^(.*) http://www.yoursite.com/$1 [R=301,L]<cfheader statuscode="301" statustext="Moved permanently"><cfheader name="Location" value="http://www.new-url.com/">
<?header( "HTTP/1.1 301 Moved Permanently" );header( "Status: 301 Moved Permanently" );header( "Location: http://www.new-url.com/" );exit(0); // This is Optional but suggested, to avoid any accidental output?>
<%@ Language=VBScript %><%Response.Status="301 Moved Permanently"Response.AddHeader "Location", "http://www.new-url.com/"%>
<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><%response.setStatus(301);response.setHeader( "Location", "http://www.new-url.com/" );response.setHeader( "Connection", "close" );%>
$q = new CGI;print $q->redirect(" http://www.new-url.com/ ");head :moved_permanently, :location => "http://www.domain.com/