Forum Moderators: coopster
I've done this, which appears to work, for the home page:
<?php if ($_SERVER["HTTP_HOST"]!= "www.example.com") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/");
exit; }
else {};
?> That sorts it out for one page, but I'd like to extend it to be placed in an include on every page. Is there a way of doing that which would redirect automatically with a 301 to the same page name in each case?
Also, is the above code the best way?
Thanks for your comments!
<?php if ($_SERVER["HTTP_HOST"]!= "www.example.com") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: [example.com...] . $_SERVER["REDIRECT_SCRIPT_URL"]);
exit; }
else {};
?>
In case anyone reads this thread later on, the full code is:
<?php
if ($_SERVER['HTTP_HOST']!= "www.example.com") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com".$_SERVER['REQUEST_URI']);
exit;
}
else { /* send whatever normal headers you need */ };
?> Note that I removed the trailing slash from the redirect URL, because
$_SERVER['REQUEST_URI'] includes it already.