Forum Moderators: coopster
list($subdomain, $domain, $tld) = explode('.', $_SERVER['HTTP_HOST']); $arr = explode('.', $_SERVER['HTTP_HOST']);
$arrLength = count($arr);
$tld = end($arr);
$domain = $arr[$arrLength - 2];
// since this might not exist, give it a fallback
$subdomain = $arr[$arrLength - 3] ? false;
$arr = explode('.', $site);
$arrLength = count($arr);
if ($arrLength == 3)
list($subdomain, $domain, $tld) = $arr;
elseif ($arrLength == 2)
list($domain, $tld) = $arr; $site = 'www.example.com';
// Test 1
$arr = [];
$tld = $domain = $subdomain = false;
$start_time = microtime(TRUE);
for ($i = 0; $i < 10000; $i++) {
$arr = explode('.', $site);
$arrLength = count($arr);
$tld = end($arr);
$domain = $arr[$arrLength - 2];
// since this might not exist, give it a fallback
$subdomain = $arr[$arrLength - 3] ? false;
}
$end_time = microtime(TRUE);
echo "Test 1: ($tld - $domain - $subdomain) ";
echo $end_time - $start_time;
echo "\n";
// Test 2
$arr = [];
$tld = $domain = $subdomain = false;
$start_time = microtime(TRUE);
for ($i = 0; $i < 10000; $i++) {
$arr = explode('.', $site);
$tld = array_pop($arr);
$domain = array_pop($arr);
// don't use this, just use $arr as an array of subdomains
// $subdomain = join('.', $arr);
}
$end_time = microtime(TRUE);
echo "Test 2: ($tld - $domain - $subdomain) ";
echo $end_time - $start_time;
echo "\n";
// Test 3
$arr = [];
$tld = $domain = $subdomain = false;
$start_time = microtime(TRUE);
for ($i = 0; $i < 10000; $i++) {
$arr = explode('.', $site);
$arrLength = count($arr);
if ($arrLength == 3)
list($subdomain, $domain, $tld) = $arr;
elseif ($arrLength == 2)
list($domain, $tld) = $arr;
}
$end_time = microtime(TRUE);
echo "Test 3: ($tld - $domain - $subdomain) ";
echo $end_time - $start_time;
So I'm guessing that when someone types in my website without the "www", the variables.php script fires for long enough to write this error, but then it redirects before the user notices.
If Apache redirects to "www" then your "variables.php" script would not be called at all (until the next request). So if "variables.php" is being called without the "www" subdomain then it implies Apache has not redirected the request for some reason.
So, to confirm, every domain should be redirected to the "www" subdomain? "subdomain.example.com" should be redirected to "www.example.com" and "example.com" should be redirected to "www.example.com"?
# potential subdomains
RewriteCond %{HTTP_HOST} !^(?:www|ww2|beta|images|i|inc|epsilon)\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] Are you only interested in the $domain? (ie. $subdomain and $tld are just a means to get at the $domain?)
I also have to consider if it takes longer for variables.php to download. So end() is 143 characters, array_pop() is 76, and list() is 164... meaning, the fastest to process also takes a few ms longer to download, so it might still take longer in the long run.
$arr = array_reverse(explode('.', $site));
[$tld, $domain, $subdomain] = [$arr[0], $arr[1], $arr[2] ?? false]; $arr = explode('.', $site);
[$tld, $domain] = [array_pop($arr), array_pop($arr)]; [edited by: phranque at 12:35 am (utc) on Jan 8, 2021]
[edit reason] fixed nulll coalescing operator for clarity [/edit]
So, in theory, this script is ONLY fired when they go to "www"... or maybe no subdomain at all.
..."subdomain.example.com" should be redirected to "www.example.com" ...
Correct, yes.
I'm assuming this means that the $tld param doesn't exist, which implies that there's no www.