When subdomains are trouble
Mon 21, November 2005 22:45:36This is a good techie tip if you are hosting your site in a “multi-domain” account (you know, when you have a single hosting account, but you can have many domains on the same IP address).
A common problem with these accounts is that they have wildcard DNS enabled, which means that any request for a sub domain that doesn’t exist gets redirected to your main page. The problem is that this will always be the main page of the main account!
Here’s an example to make it clear. Let’s say that you have the domains abc.com and xyz.com on the same account. abc.com is your main domain (the one you used to register…). If you type in your browser 123.abc.com, you will get the homepage of abc.com, which is exactly what you want (I’m assuming that the 123 sub domain doesn’t exist). But if you type 123.xyz.com, you will not get the homepage of xyz.com (as you might have expected), but again the homepage of abc.com - no fun.
Now, there are a couple of options to deal with this. You can disable the wildcard DNS, or mess around with the httpd.conf files, but there are always some loss of functionality if you do this.
For my specific case, I came up with an alternative solution, that doesn’t involve messing around with any configuration whatsoever. You can use a small piece of PHP to interpret the request and then redirect if needed. The basic idea is that if the page sees that the “intended” domain is different, then it redirects the page by sending a new location.
Here’s an example (you have to adapt it to your specific situation):
<?PHP
// Handy function to test end of strings
function endsWith( $str, $sub ) {
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub );
}
$requested_host = $_SERVER['HTTP_HOST'];
// If the request was meant for xyz.com
if(endsWith($requested_host, "xyz.com")) {
header("Location: http://xyz.com");
}
// Anything else goes here
else {
header("Location: http://abc.com");
}
?>
This may be slightly different for you case, and can even be improved to handle “any” domain, but I think you get the point.
Categories: Tips 4 Geeks
2 Responses to “When subdomains are trouble”
[...] iRui.ac - Home of vSlider » Blog Archive » When subdomains are trouble 当子域名出现了问题 (tags: domain 虚拟主机) [...]
[...] iRui.ac - Home of vSlider » Blog Archive » When subdomains are trouble 当子域名出现了问题 (tags: domain 虚拟主机) [...]
Care to comment?