Forum Moderators: coopster

Message Too Old, No Replies

Can you PHP 301 redirect subdomains to www domain?

         

internetheaven

9:21 am on Apr 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've closed down several sites and have created a script to forward all incoming visitors to the only existing page left - the main page:

$wwwcheck = $_SERVER['HTTP_HOST'];
if(preg_match("/website1/i", $wwwcheck))
{
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: [website1.net...] );
}
elseif(preg_match("/website2.net/i", $wwwcheck))
{
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: [website2.net...] );
}
else
{
echo $notfound;
}

with not found having a code to serve up a message. Obviously, I repeat the code several times, one for each closed site.

But subdomain.website1.net or archive.website2.net is not redirecting to the main page. It returns a 404 error.

Can I get round this in PHP?

Thanks
Mike

incrediBILL

9:25 am on Apr 4, 2011 (gmt 0)

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



Did you actually remove the subdomains or alter your DNS?

That would explain an actual 404.

internetheaven

9:40 am on Apr 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, there are no subdomain records now. I've closed the server these domains were on, am using a cheap host and need a few scripts to upload to manage the many sites that were closed down.

I've got everything else covered, it's just these several that had subdomains that I can't work out how to redirect. I could probably code it in htaccess but I don't have that any more.

incrediBILL

10:45 am on Apr 4, 2011 (gmt 0)

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



My point was if you remove blah.example.com from the server it doesn't automatically point blah.example.com to example.com, you need a DNS entry pointing blah.example.com to example.com first, then you can run a redirect script.

In other words, the subdomains you removed need to be pointed to the primary domain in DNS.

subdomain1.example.com -> example.com
subdomain2.example.com -> example.com

so on and so forth

Once you've done that you can perform the redirects.

internetheaven

4:29 pm on Apr 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Like I said, I don't have control over that any more. So that means PHP can't accomplish this?

Okay ... time to start searching for another cheap host ...

rocknbil

4:46 pm on Apr 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can't you use mod_rewrite instead?

incrediBILL

5:35 pm on Apr 4, 2011 (gmt 0)

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



Like I said, I don't have control over that any more.


Ask your host, it takes literally 2 seconds for them to drop in a couple of DNS records on your main domain.

Some hosts provide this capability for users via a DNS control panel per account, sure you don't have one?

If the host doesn't provide this, you can roll your own DNS with services like ZoneEdit, pretty simply stuff really.

Can't you use mod_rewrite instead?


That won't work until the DNS points to the domain first, it was a separate hosting account if I understand correctly, which was disconnected and now there is no pointer for that subdomain.

Hosting company should be able to easily add a pointer.

Here's a simple real world example of what's happening:

Using the "host" command in linux, I did two DNS lookups for example.com:
host example.com
example.com has address 192.0.32.10
example.com has IPv6 address 2620:0:2d0:200::10

host subdomain.example.com
Host subdomain.example.com not found: 3(NXDOMAIN)


Note how just example.com resolves to an IP address but subdomain.example.com does not because there is no DNS record for subdomain.example.com, therefore it can't get to the server, therefore Apache rewrites nor PHP scripts work until the DNS is resolved.