Forum Moderators: coopster

Message Too Old, No Replies

$ SERVER['HTTP HOST'] won't work properly

         

internetheaven

10:12 pm on Feb 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I understand it, $_SERVER['HTTP_HOST'] should give me the web address of the calling website. But when I do this:

<?php
$wwwcheck = $_SERVER['HTTP_HOST'];
if(preg_match("/website1.com|website2.co.uk/", $wwwcheck))
{
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http:// www.newwebsite.com/" );
}
elseif(preg_match("/example.com|example2.org/", $wwwcheck))
{
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http:// www.otherwebsite.co.uk/" );
}
else
{
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http:// www.3rdsite.org/" );
}
?>

whilst website1.com, website2.co.uk etc. redirect just fine, things like website1.com/folder or example2.org/file.html result in an error.

Anyone know why that might be?

TheMadScientist

11:43 pm on Feb 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're not passing the REQUEST_URI to the new location, so it's working as it should... You're sending all requests to the root of the new location:

header( "Location: http://www.example.com".$_SERVER['REQUEST_URI'] );

Also, you should escape the dots in the regex:

elseif(preg_match("/example\.com|example2\.org/", $wwwcheck))

internetheaven

7:37 am on Feb 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're not passing the REQUEST_URI to the new location, so it's working as it should


Sorry should have been clear. I actually want website1.com/folder or example2.org/file.html to redirect to the root of the new site.

internetheaven

7:59 am on Feb 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not a PHP problem at all in the end. I realised that I had not set up the htaccess file on the virtual hosting to forward all requests to the index.php for processing with that script I posted above:

RewriteRule ^(.*) index.php

in the htaccess file has fixed everything. Sorry guys!

TheMadScientist

5:56 pm on Feb 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I actually want website1.com/folder or example2.org/file.html to redirect to the root of the new site.


AFAIK what you're doing is usually recommended against as far as rankings are concerned... I'd try to go to a similar page on the new site and make sure you get there in one redirect, but either may not be possible. Just a thought if SE rankings are a concern.

internetheaven

10:36 pm on Feb 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



make sure you get there in one redirect


With my limited server/php knowledge I probably should ask if this is 1 redirect or not:

1. olddomain.com nameservers point to an IP address
2. at which resides redirectingdomain.com which is virtually hosted on plesk as the default domain
3. thus (I believe) making olddomain.com call the information from redirectingdomain.com/index.php
4. that php file issues a 301 redirect to newdomain.com

is that one redirect?

what you're doing is usually recommended against as far as rankings are concerned


The new website has a completely new structure, mostly new content and many doorway pages merged together where they are closely related and uniqueness was thin.

301 redirecting 500+ individual pages from the old site into the remaining 80-ish seemed like a monumental task.

Do you mean that rankings for the individual inner pages would be lost only? Or do you mean that the main page would be de-ranked as all the old pages were being redirected to it?