Return to Snippet

Revision: 32516
at September 27, 2010 19:00 by r4b87


Updated Code
$domain=$_SERVER['HTTP_HOST'];

if(substr($domain, 0, 3)!='www'){
    $redirect_url="http://www.{$domain}{$_SERVER['REQUEST_URI']}";

    if($_SERVER['QUERY_STRING']!=''){
        $redirect_url.="?{$_SERVER['QUERY_STRING']}";
    }

    header('HTTP/1.1 301 Moved Permanently');
    header("Location: {$redirect_url}");
    exit();
}

Revision: 32515
at September 27, 2010 18:58 by r4b87


Initial Code
$domain=$_SERVER['HTTP_HOST'];

    if(substr($domain, 0, 3)!='www'){
        $redirect_url="http://www.{$domain}{$_SERVER['REQUEST_URI']}";

	if($_SERVER['QUERY_STRING']!=''){
	    $redirect_url.="?{$_SERVER['QUERY_STRING']}";
	}

	header('HTTP/1.1 301 Moved Permanently');
	header("Location: {$redirect_url}");
	exit();
}

Initial URL


Initial Description
This code wil 301 redirect non-www URLs to the www URL

Initial Title
Redirect non-www URLs

Initial Tags
php, redirect

Initial Language
PHP