Preparing Your New Site for Structural Changes - Improper Redirect: Simple Redirection Header
(Page 3 of 6 )
When the new system has determined that it can successfully intervene on the visitors’ behalf, it must redirect the visitor (or spider) to the appropriate content. There are a few ways this can be done. We are going to use a location header (php) and a HTTP status code in this article. To get started, let's look at what happens when we create a redirect on a page. Take a look at the following code:
<?
//redirect.php
header("Location: http://www.example.com/");
?>
This snippet of code will automatically redirect the visitor to http://www.example.com. This is done by setting a 302 header.
To FULLY understand this, we must take a look at the actual text being passed back and forth in the background. I suggest for anyone who wants to better understand the nuts and bolts of the HTTP underpinnings, pick up a free copy of ethereal. Ethereal is a packet sniffing (er..network protocol analyzer) application that will let you see every packet that passes over the wire for each request. Ethereal can also be very helpful when troubleshooting cookies and other headers being passed back and forth.
Here is a snippet from the actual exchange that took place when I requested http://www.hafenbrack.com/redirect.php
GET /redirect.php HTTP/1.1\r\n
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n
Accept-Language: en-us\r\n
Accept-Encoding: gzip, deflate\r\n
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n
Host: www.hafenbrack.com\r\n
Connection: Keep-Alive\r\n
Cookie: ClickTrack=1.1.1.1.14477107221282637\r\n
\r\n
You can see that my browser requested the file /redirect.php and passed a few headers to the server. The line we are interested in here is the GET line. It shows the file originally requested.
Now, let's take a look at how the server responded.
Below is a snippet from the actual exchange that took place when the server fulfilled my request for /redirect.php
HTTP/1.1 302 Found\r\n
Date: Tue, 23 Dec 2003 20:53:46 GMT\r\n
Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) mod_jk/1.2.0 mod_perl/1.26 PHP/4.2.2 FrontPage/5.0.2 mod_ssl/2.8.12 OpenSSL/0.9.6b\r\n
Set-Cookie: ClickTrack=1.1.1.1.14477107221282637; path=/; expires=Tue, 23-Dec-03 21:53:46 GMT\r\n
X-Powered-By: PHP/4.2.2\r\n
Location: http://www.example.com/\r\n
Keep-Alive: timeout=15, max=100\r\n
Connection: Keep-Alive\r\n
Transfer-Encoding: chunked\r\n
Content-Type: text/html\r\n
\r\n
Do you see what happened here? The server received the request, passed back a header with a status code of Found 302 (moved temporarily). While this would work fine for browsers, this is not really what we want in our case, as we want to tell search engine spiders that the content has moved PERMANENTLY to a new location. To do this, we must employ the 301 header response code.
Next: Proper Redirect - Redirection with 301 Code >>
More Search Optimization Articles
More By Bill Sterzenbach