Page 1 of 1

'header 301 Moved Permanently' redirects twice?

Posted: Wed Jul 05, 2006 5:35 am
by davinci
Situation:

I have multiple domains in Apache which are aliasses for the 'main-domain'
I want to make sure that Google only indexes this maindomain.
I do not want use mod_rewrite or something for this.

I add the folowing to index.php:

Code: Select all

<?
if (substr($HTTP_HOST, 4)!="maindomain.com") 
{ 
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.maindomain.com/index.php");
exit;
}
?>
But when i do a wget:

Code: Select all

C:\>wget www.aliasdomain.com
--11:37:17--  http://www.aliasdomain.com:80/
           => `index.html.1'
Connecting to www.aliasdomain.com:80... connected!
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.maindomain.com/index.php [following]
--11:37:17--  http://www.maindomain.com:80/index.php
           => `index.php.1'
Connecting to www.maindomain.com:80... connected!
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.maindomain.com/index.php [following]
http://www.maindomain.com/index.php: Redirection to itself.
It does get redirected to the main-domain, but then he tries to redirect it again. It looks like it doesn't follow the if-statement and tries to send the header again.
Is this normal behaviour? Or is there a solution for this?

Posted: Wed Jul 05, 2006 5:55 am
by Jenk
try:

Code: Select all

<?php
if (strpos($_SERVER['HTTP_HOST'], 'maindomain.com') === false) {
    //redirect
}
?>

Posted: Wed Jul 05, 2006 6:11 am
by davinci
that did the trick,
i know using substr is a quick and dirty way!