Doing Page Redirects the Right Way
Doing Page Redirects the Right Way
Redirecting webpage requests is useful when you’ve deleted outdated pages, or when moving your site to a new domain. Rather than give site errors to visitors, and tempt them to leave your site, you should make sure they are redirected to a page on your website.
There are several ways of doing this. We recommend server-side 301 redirects in most cases but there’s also a method that uses HTML that could be used in a pinch, and especially when you don’t have full server access.
HTML Meta Redirect
The HTML meta element can be used to redirect the user to another page after a specified number of seconds.
Place the following in the <header> tags:
<meta http-equiv=”refresh” content=”5;URL=’http://www.example.com/newPage'” />
Within the “content” field, the first number specifies the number of seconds it takes to redirect, with “0” being an immediate redirect. Having a delay could be helpful if you want to let visitors know that a certain page or site has been removed, and that they’re being redirected to another location.
But there are many reasons to avoid HTML meta redirects. They are not particularly good for maintaining search engine ranking. They also often break the “back” button, so when the user tries to go back, it triggers the refresh, sending them back to the original page. Also, it might not work with all user agents such as screen reader software according to W3C.
Server-Side 301 Redirect Scripts
Server-side scripts or configuration file can send an appropriate redirect status code (for instance, “301” means that a page has permanently moved to a new location) and a location header specifies another URL for redirection. When the browser receives this response, the location bar changes and the browser makes a request with the new URL.
There are several ways of doing 301 redirects.
PHP Header Method
In PHP, developers can send a raw HTTP header with the header method. The code below sends a 301 status code and a new location. If no status is explicitly set, it sends a 302 status code, which basically specifies that it’s a temporary redirect and the new path is not cached in a visitor’s browser.
header(“HTTP/1.1 301 Moved Permanently”);
It’s important to note that the header() function must be called before any actual output is sent for the redirect to work.
You can also use PHP’s power as a programming language to automatically redirect pages such as pages not found on your website. For that particular situation, SitePoint provides a tutorial on creating a PHP script that sends the site visitor to the correct page or similar content.
The Apache Redirect Method
If you have access to make changes to the .htaccess file on your Apache server, you can do simple Apache redirects. You need to locate the .htaccess file and open it in a text or code editor.
You’ll be making the redirects using the following basic syntax:
Redirect [status] URL-path URL
redirect 301 /oldPage.html http://www.example.com/newPage.html
After “301”, specify the file or the folder to be redirected, and then the destination.
For more elaborate Apache redirection, you might want to look into how to do URL rewrites. Apache also provides a guide to common rewrite scenarios.
W3 describes how to do server redirects in Java Servlets or JavaServer Pages (JSP), and in Active Server Page (ASP) with VBScript in its online documentation. And 301 redirects can be done in other languages such as Ruby on Rails.
A Note on Staying Search Engine-Friendly
In terms of keeping pages optimized for search engines, Google says that 301 redirects are the best way to ensure that users and search engines are directed to the correct page. They capture the intended traffic from that link for the page it redirects to.
According to Google’s Matt Cutts, there is no limit to how many 301 (or permanent) redirects you can do between one page and another. But redirecting multiple times (ie. hopping from page 1 to page 2 to page 3, etc.) can cause the Google crawler to give up after a few hops and also increase the overall page load time. Cutts recommends limiting redirects to a maximum of around 4 or 5 hops.
To redirect people who access your site through several different URLs, for instance, . http://example.com/home, and http://home.example.com to a preferred (or “canonical”) destination, then you would also set your preferred domain in Google Webmaster Tools.
Page redirects can send visitors to the most current resources, but also help when, for instance, someone sent out an incorrect link URL. For these reasons alone, page redirects can be very common. And when it’s necessary to redirect pages, it’s best to use server-side 301 redirects because they’re less likely to confuse users as well as search engine crawlers.