Forum Moderators: coopster

Message Too Old, No Replies

Simple mod_rewrite equivalent with PHP

Redirecting to new domain name

         

encyclo

12:48 am on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a site which is visible with three different domain names that I'd like to consolidate into one. Unfortunately, I've got a bug with mod_rewrite on the server, so I'm looking for a way with PHP.

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!

Timotheos

4:21 am on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's auto-prepend -file [us4.php.net] for including a file on every page.

<?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 {};
?>

mincklerstraat

7:35 am on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Timotheos's answer looks like the way to go; but if this doesn't do it, you can try replacing
$_SERVER["REDIRECT_SCRIPT_URL"]
with:
$_SERVER['REQUEST_URI'] (this might have been what Timotheos meant, I've never seen the above server var).

encyclo

1:00 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys, that does the job!

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.