The 2 most comment types of redirects are the permanent redirect (301) and the temporary redirect (302). There are some other types of redirects, but for the most part you should never need to use them. Introduced in HTTP 1.1 is a new temporary redirect (307). For now I would stay away from using it just in case a bot decides to crawl your site using HTTP 1.0 requests.
Permanent redirects are great if you have moved a page on a server or if you have moved to a new domain name. This way all of your page rank is transferred through to the new page. Using the permanent redirect is search engine friendly.
Temporary redirects are good for affiliate links or links that may change around from time to time. No page rank is passed through a temporary redirect.
Below are some examples of how to use PHP and .htaccess to achieve these redirects. But obviously, don’t stick all of those header() calls at the top of one page, you only need one. Also you can redirect off your domain to a new one as well, but remember to use a temporary redirect unless you know for sure it is a permanent move.
The .htaccess can become crazy with regular expressions to match which file(s) get redirect. It’s just a really simple example to give you an idea to get you started. If you are looking for more do some searches and you should be able to find some great examples. I’d link to some but it can be really specific. I have included 2 examples at the bottom for using .htaccess to either remove or add the www if you want.
Using PHP
<?php
// 301 Moved Permanently
header("Location: /foo.php", TRUE, 301);
// 302 Found
header("Location: /foo.php", TRUE, 302);
header("Location: /foo.php");
// 307 Temporary Redirect
header("Location: /foo.php", TRUE, 307);
?>
Using .htaccess
RewriteEngine On
RewriteBase /
RewriteRule (.*) http://www.domain.com/$1 [R=301, L]
RewriteRule (.*) http://www.domain.com/$1 [R=302, L]
RewriteRule (.*) http://www.domain.com/$1 [R=307, L]
Redirect domain.com to www.domain.com
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301, L]
Redirect www.domain.com to domain.com
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301, L]