The Hitchhiker’s Guide to URL Redirection and Affiliate Link Cloaking – Part 3

by Phil on November 6, 2010

C. PHP Redirect

Most people like using PHP, because there is a ton you can do with it, and it is processed on the server before it reaches the user’s browser, resulting in a very quick and search engine friendly redirect.
PHP redirection is what I personally use, mainly because of its flexibility. As you can see by the sample code below, redirecting with PHP is extremely simple and consists of only 3 elements:
1. An opening PHP tag,
2. a location you want the page to be redirected too,
3. a closing PHP tag.
When someone tries to access the page that the code below is placed on, they will be automatically redirected to the URL specified in the code.
<?php
header(“Location:http://www.yoursite.com”);
?>
This code will send a HTTP header back to the browser. The header tells the browser to move to another URL location instantly. For those who want to know, a PHP redirect sends a 302 (standard) HTML code.
It’s important to remember to not have any text before this script or it won’t work. I would not recommend including an additional content besides the script above on the redirect page.
A PHP redirect can also be done using the similar method as the META refresh redirection method explained in part 1.
D. Webserver Redirect

The .htaccess file
.htaccess (Hypertext Access) is the default name of Apache’s directory-level configuration file. It allows webmasters to customize configuration directives, normally available in the main httpd.conf (see next section).
A .htaccess file is a file that is being looked up and interpreted by your webserver before serving any other page located in your directory. It usually contains special directives.
.htaccess allows webmasters to do a range of customization to a webservers behaviour in a directory, including password protecting them, denying access, error handlers, redirects and a lot more. .htaccess is particularly useful when you don’t have root access to the server. For example, in virtual Web Hosting and ISPs.
For instance, in order to specify your own customized error documents, one can simply add the following command (all on one line) within a .htaccess file:
ErrorDocument code /directory/filename.ext
or
ErrorDocument 404 /errors/notfound.html
This would cause any error code resulting in 404 to be forwarded to yoursite.com/errors/notfound.html
Likewise with:
ErrorDocument 500 /errors/internalerror.html
You can name the pages anything you want and you can place the error pages anywhere you want within your site, as long as they are web-accessible (through a URL). The initial slash in the directory location represents the root directory of your site, that being where your default page for your first-level domain is located.
If you were to use an error document handler for each of the error codes, the .htaccess file would look like the following (note each command is on its own line):
ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authreqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/serverr.html
You can specify a full URL rather than a virtual URL in the ErrorDocument string (http://yoursite.com/errors/notfound.html vs. /errors/notfound.html). But this is not the preferred method by the server’s happiness standards.
You can also specify HTML code:
ErrorDocument 401 “<body bgcolor=#ffffff><h1>You can’t touch this!”

Instead of using the ErrorDocument directive in the .htaccess file to direct the visitor to a specific 404 page, when he or she tried to visit an unexisting page on your site, you can use this functionality to redirect them on purpose.
Example:
ErrorDocument 404 http://www.domain.com/redirect.cgi
The redirect.cgi script could then be a script that retrieves the visitor’s parameters and redirects them to a specific page depending on those parameters.
Below is for instance the code used by the GoTryThis software from John Reel.
“ErrorDocument 404 /gotrythis/gotrythis.php” in the .htaccess file
There’s another way you can hook a custom error page or redirection into your site too, using an httpd.conf file, as explained in the next section.
Before making any of these configurations, however, the following points need to be kept in mind.
Your webserver Administrator should allow you to make these changes by using “AllowOverride All” in the main configuration file(httpd.conf)
You need to make sure that you are not using Microsoft Frontpage on your website. Frontpage uses .htaccess for its own directives. Changing the .htaccess files to insert new directives “will” break your website.
Test, Test, Test. Test new .htaccess configurations on an empty directory before making it LIVE.
A .htaccess file controls the directory it is in, plus all subdirectories. However, by placing additional .htaccess files in the subdirectories, this can be overruled. Therefore, if you have a .htaccess file in a subdirectory and another one in a parent directory, the one in the subdirectory will be followed.
I could probably write a whole guide in itself on the different ways you can use .htaccess redirects, however below are 3 different methods you can use .htaccess redirection for. Like PHP redirection, using a .htaccess redirect is great because there is no delay, since your browser checks the .htaccess file before it even checks the destination of the URL.
Warning: This type of redirection will not work on windows servers, and ALWAYS upload .htaccess files in ascii mode or else you can mess up your server.
YOU HAVE BEEN WARNED!

Basically all you need to do is create a file named .htaccess using a text editor like notepad, include the specified code below and then upload the .htaccess to the particular folder on your server your webpage or site is in. This method is very search engine friendly.
Below are some useful 301 Redirect examples:
Move a Single Page
Redirect 301 /old.htm http://www.yoursite.com/new.htm

Move a Site
Redirect 301 / http://www.yoursite.com
Change File Extension
RedirectMatch 301 (.*)\.html$ http://www.mysite.com$1.php
In addition of using the 404/301 directive, you can use .htaccess to redirect users to a different URL. The .htaccess looks for any request for a specific page and if it finds that request, it forwards it to a new page you have specified.
The syntax is:
(i)redirect  (ii)accessed-file (iii)URL-to-go-to
There are 3 parts:
(i) the redirect command,
(ii) the location of the file/directory you want redirected, and
(iii) the full URL of the location you want that request sent to.
These parts are separated by a single space and should be on one line.
For example, if you want to redirect users from oldfile.html in the directory of your account, myacc, to newpage.html, the syntax should be:
redirect /~myacc/oldfile.html http://myacc.com/newpage.html
Anyone going to /~myacc/oldfile.html will end up on http://myacc.com/newpage.html.
Please note that the URL has to be full, even if you are going to send the users to another page on your own site.
You can even redirect an entire directory this way:
redirect /~myacc/old_dir/ http://myacc.com/new_dir/

In this part, we covered a lot. Tomorrow I’ll talk about the last methods of redirections and then we’ll tackle the cloaking.

Leave a Comment

Previous post:

Next post: