You are about to read a tutorial written by a member of this website. Although the author probably invested every effort to make this tutorial's content correct, we can not guarantee that the information on this page is 100% exact. Please keep this in mind when reading this tutorial. ;o)
Good morning, good afternoon, good evening, and for the geeks, good night.

If you've got a website, that you're interested in Google's
PageRank, you know that more external links you add, lower will be your PageRank. So how to do ? It's easy : you delete the external links !
Are you crazy ? How do you want me to put my partners' links ?
Ok, ok, read this course.
We're going to do it this way :
- you click on a link
- PHP redirects you to the corresponding page
Is that all ?

Yes, it is.
We're going to use the PHP function header(). We're going to proceed this way ($final_url is the URL we want to reach) :
Code: PHP1
2
3 | <?php
header('Location:'.$final_url);
?>
|
Ready ? Let's go !
First, you're going to have to rewrite your links on this pattern :
- domain.tld is your domain name (such as www.newbiesparadise.com)
- out.php is the page that is going to help you to reach the page (you can give it another name if you want)
- final_url : the URL of the website you want to reach (such as www.google.com)
Now, let's deal with the redirection part.
Here we are.

Now, create a page called out.php in which we're going to insert the redirection code.
Remember : first we transfer the final URL, so we have to test if there is one (logical, isn't it ?

).
Code: PHP 1
2
3
4
5
6
7
8
9
10
11
12 | <?php
if (isset($_GET['url']))
{
//if everything is OK
}
else
{
//sombody tried to access out.php
}
?>
|
Reminder : we are using this PHP function : header('Location:'.$final_url) ! So :
Code: PHP 1
2
3
4
5
6
7
8
9
10
11
12
13
14 | <?php
if (isset($_GET['url']))
{
$final_url = $_GET['url'];
}
else
{
$final_url = 'http://www.your_home_page.com';
}
header('Location:'.$final_url);
?>
|
If you tried it at home, 2 different things can have happened :
The first case happens when you haven't declared the HTTP protocol. That's why the server is looking for the file "final_url". Anyway, it's useful to redirect the user during a maintenance, for example.
But what we want is case n°2. Once again, you can do it two ways :
- the long one : you add http:// before each external link from your website.
- the quick one : you ask PHP to do it.
If you're normal, you must have chosen the second one. We had written that before :
Code: PHP 1
2
3
4
5
6
7
8
9
10
11
12
13
14 | <?php
if (isset($_GET['url']))
{
$final_url = $_GET['url'];
}
else
{
$final_url = 'http://www.your_home_page.com';
}
header('Location:'.$final_url);
?>
|
We merely add http:// if $final_url hasn't got it :
Code: PHP 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | <?php
if (isset($_GET['url']))
{
$final_url = $_GET['url'];
}
else
{
$final_url = 'http://www.your_home_page.com';
}
$http = strpos($final_url,'http://');
if ($http === false)
{
$final_url = 'http://' . $final_url;
}
header('Location:'.$final_url);
?>
|
If you want to know more about the strpos() function, you can go there. 
Now, you know how to use the PHP redirection. Of course, you can use it for other uses, such as, in the administration part of your website, if the user hasn't written the correct password.