SEO Friendly/Aware Redirect of a URL

Updated CodeAngry 0 Tallied Votes 570 Views Share

SEO URL Redirection Basics: TUTORIAL

First you can see/get/use some code here.

<?php
/**
* Redirect the visitor to another URL using a proper status code.
* 
* @param string $URL
* @param int $StatusCode
* @copyright Claude "CodeAngry" Adrian
* @license http://www.wtfpl.net/
*/
function RedirectURL($URL = null, $StatusCode = 302){
    // Validate arguments
    if(empty($URL)){
        $URL = $_SERVER['REQUEST_URI'];
    }
    if(!is_string($URL) or !strlen($URL = trim($URL))){
        throw new \InvalidArgumentException('$URL must be a non-empty trimmed string.');
    }
    if(empty($StatusCode)){
        $StatusCode = 302;
    }
    if(!is_numeric($StatusCode) or (intval(($StatusCode = intval($StatusCode)) / 100) != 3)){
        throw new \InvalidArgumentException('$StatusCode must be a 3## HTTP status.');
    }

    // Do http redirect if headers are not sent
    if(!headers_sent()){
        header("Status: {$StatusCode} Relocated");
        header("{$_SERVER['SERVER_PROTOCOL']} {$StatusCode} Relocated");
        header("Location: {$URL}");
    }

    // If we have a move, add the rel canonical in case anything broke and headers failed to send
    if($StatusCode === 301){
        // http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394
        echo '<link rel="canonical" href="', htmlentities($URL), '"/>', PHP_EOL;
    }

    // Do JS redirect and Meta redirect (as plan B)
    echo '<script type="text/javascript">window.location.href="', $URL, '";</script>', PHP_EOL;
    echo '<meta http-equiv="refresh" content="0;url=', $URL, '" />', PHP_EOL;

    // Add a clickable link (as plan C)
    echo '<div><a href="', htmlentities($URL), '">Click here to continue &hellip;</a></div>', PHP_EOL;
    // ^ Comment out or edit at will

    // Die here (to prevent further output)
    die; // Comment out this at will
}

/**
* Alias of RedirectURL($URL, 301).
* 
* @param string $URL
* @copyright Claude "CodeAngry" Adrian
* @license http://www.wtfpl.net/
*/
function MoveURL($URL = null){
    return RedirectURL($URL, 301);
}
?>

Redirecting a URL is easy. Just add a Location: header and you're done.

NOT REALLY

In SEO, a bad redirection can kill your rankings. So let's have a short discussion on redirections.

A redirection should NEVER be a naked Location:. It should always carry an HTTP Status Code and for SEO, the most meaningful are 301 and 302. (not)Knowing them can make or break your rankings.

Redirection status codes are in the 3## range. But 301 and 302 matter:

301 - Moved Permanently

This status code tells the search engine crawler that a URL moved to a new location. The search engine is to redirect all link juice (incoming link power) to the new URL mentioned in the Location: header. The crawler will return less often to the 301-ed URL. For the link juice flow to stay redirected, your 301 needs to stay in place. 301 is search engine friendly.

302 - Temporary Redirect

This status code tells search engines that the page has temporarly relocated to another URL. The search engine is not to forward link juice and is to keep crawling this page until it returns to normal. 302 should not be sent to search engines!

In real SEO life

Redirections with 302 are only made on POSTs. After you process a submitted form you use a 302 to redirect back and ditch the POST payload. Redirections with a 301 are only done for SEO purposes. This means a site relocation, redesign, restructuring...

If you got comments, question... I'm around :)

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

PS: I have no idea how to put this in Tutorials...

Use "flag bad post" and request that an admin change the thread.

My only addition would be to link to the source for status codes, not wikipedia.

Gideon_1 15 Junior Poster

Not all that understandable but thanks for your time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.