C and S Design.
Search Friendly Programming and Design

Go to -> C and S Design -> Articles -> Coding -> Redirect -> Show Code

Click here to view or download the code for this example


Coding Redirects


Why Redirect?

Redirect Image
Knowing how to redirect sites and pages safely is an essential part of owning, operating or building websites. No matter how well thought out the original design there will come a time when something needs to be changed. There is also the fact that several hostnames can point to the same site and should be redirected so that they will all appear as one domain to user agents.
Redirecting can be accomplished in several ways using either client-side and server-side code.

The best way is to implement redirects using server side code, this will ensure that all user agents will action or follow the redirect. If it is implemented client side using meta refresh or javascript some user agents and browsers with scripting disabled will not be transfered to the new location.

Using any server side code can give a greater range of options when redirecting user agents. The advantage is that any redirection is set before any output is sent to the user agent and will be identical for all types of devices.

For redirects there are mainly two HTTP response codes that all user agents respond to. A 301 for a permanent redirect and a 302 response which is always take to be a temporary redirect. This for HTTP/1.1 user agents is incorrect. Under the HTTP/1.1 protcol (RFC2616 sec 10), a temporary redirect response is a 307 response code and a 302 response is Found. There is also a 303 (See Other) response which again is a indication that a resource is in a different location to the one requested. For this article though we will use the generally accepted (HTTP/1.0) responses of 301 (Moved Permanently) and 302 (Found). To learn more about RFC2616 Response Codes, A W3C article on the HTTP/1.1 Response Codes is available.


Valid HTML 4.01! Valid CSS! copyright © C and S Design 2004 - 2005
Website Design and SE Friendly Coding C and S Design