Redirecting URLs
Redirecting one URL to another URL is part of life if you have ever managed a large web site for any period of time. Site redesigns, platform changes, name changes and marketing campaigns all will require redirects.
Redirect types
Like most HTTP response codes, the type of redirect really does matter. The various technologies that read the response code of the redirected URL may store it's encounter with the URL differently based on which response code is returned. For SEO purposes, you want to get your redirect response codes correct.
There are several redirect types (see W3C Status Code Definitions) but the two you'll probably work with most often are the Moved Permanently (301) and the Found (302)
301 Permanent
This response means the original URL should no longer be used, instead use the new one. It's a redirect that is intended to be forwarded to a new URL for the long term. Search engine crawlers may store this response and adjust future references to the original URL to reflect the redirection.
A good example of a Permanent redirect is forwarding mydomain.com to www.mydomain.com.
$ curl -sLD - google.com -o /dev/null
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
...
HTTP/1.1 200 OK
http://www.google.com/
Or redirecting your 5 billion dollar domain to another domain.
$ curl -sLD - broadcast.com -o /dev/null
HTTP/1.1 301 Redirect
Location: http://www.yahoo.com/
...
HTTP/1.1 301 Moved Permanently
Location: https://www.yahoo.com/
...
HTTP/1.1 200 OK
301 redirect executed in web server configuration
Apache http.conf (or htaccess) using mod_rewrite
A redirect to the www. subdomain might look like this
RewriteCond %{HTTP_HOST} mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
For the google.com response
RewriteCond %{HTTP_HOST} google\.com [NC]
RewriteRule ^(.*)$ http://www.google.com/$1 [L,R=301]
301 redirect executed in server-side code
PHP
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location:http://www.mydomain.com");
exit;
?>
JSP
<%
String redirURL = "http://www.mydomain.com/";
response.sendRedirect(redirURL);
%>
ASP.net
<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(“Location”,”http://www.mydomain.com”);
}
</script>
302 Found (Temporary)
This response means the original URL is forwarded to a different URL temporally. This response should not be saved or cached (unless otherwise directed in the HTML head), the redirect is short-term. This type of redirect is frequently done on the browser client with a message that the page or resource is redirecting and where it's redirecting to.
302 Redirect in the client
HTML HEAD using meta-refresh
<html>
<head>
<meta http-equiv="refresh" content="0; url=http://www.mydomain.com/"
</head>
</html>
Javascript redirect
<html>
<head>
<script type="text/javascript"> function redirectURL()
{
window.location="http://www.mydomain.com";
}
</script>
</head>
<body onLoad="setTimeout('redirectPage()', 5000)">
<p>
You will be redirected to <a href="http://www.mydomain.com">www.mydomain.com</a> in 5 seconds
</p>
</body>
</html>
Full list of 3xx Redirect codes
see W3C Status Code Definitions
Code | Description |
---|---|
300 Multiple Choices | The requested resource corresponds to any one of a set of representations, each with its own specific location, and agent- driven negotiation information (section 12) is being provided so that the user (or user agent) can select a preferred representation and redirect its request to that location. |
301 Moved Permanently | The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. |
302 Found | The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. |
303 See Other | If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. |
305 Use Proxy | The requested resource MUST be accessed through the proxy given by the Location field. The Location field gives the URI of the proxy. |
307 Temporary Redirect | The requested resource resides temporarily under a different URI. Since the redirection MAY be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field. |