Redirect a Page Using PHP

Sometimes, you just need to redirect one page to another.

Simple PHP redirect:

<?php header('location: http://www.yoursite.com/newpage.html'); ?>

It’s important to remember that this has to be the first output in the document for it to work.

A quick and dirty redirect that works if headers already sent:

 function forceRedirect($url = '/') {
    if (!headers_sent()) {
        header('HTTP/1.1 301 Moved Permanently');
        header('Location:'.$url);  
        header('Connection: close');
        exit;
    } else {
        echo 'location.replace('.$url.');';
    }
    exit;
}

// Usage:
forceRedirect('http://www.yoursite.com/newpage.html');

And finally, another function that does the same job:

 function redirect($url) {
    if ($url == -1) {
        $url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : '/';
    }
    Header("Location: $url");
    die();
}

// Usage:
redirect('http://www.yoursite.com/newpage.html');