I want to set the title depending on the subdomain for this I write a function, but for some reason it does not work. Tell me why?

if($_SERVER['REQUEST_URI'] = "/test/"){ $url = $_SERVER['HTTP_HOST']; $listDomain = array( "k.example.com"=>"В k", "s.example.com"=>"В s", "v.example.com"=>"В v", "t.example.com"=>"В t", "b.example.com"=>"В b" ); function seoDomen($url){ foreach($listDomain as $k=>$v){ if($url == $k){ $APPLICATION->SetPageProperty("title", $v); $APPLICATION->SetPageProperty("description", $v); $APPLICATION->SetPageProperty("keywords", $v); return false; } } } seoDomen($url); } 
  • You can also split the domain and extract the subdomain $n = explode('.', $_SERVER['HTTP_HOST']); - Mr. Black
  • of course you can, however, this does not solve the problem - ChromeChrome
  • If there is a subdomain, is it a problem to set it in the title? - Mr. Black
  • I don’t need to write a subdomain in the title, if you can, answer why my function doesn’t work - ChromeChrome
  • I am not familiar with Bitrix, however, neighbors at stackoverflow.com have the answer how to change the title even after sending the headers - Mr. Black

1 answer 1

The seoDomen function seoDomen nothing about the $listDomain variable - the wrong scope. It is necessary to transfer the initialization of the variable inside the function.

If you enable error output, you should immediately see it. Also, normal IDEs highlight such an error.

And the same with $APPLICATION . The function does not know about it. It is necessary to transfer as argument to function. Or even remove the use of the function:

 if ($_SERVER['REQUEST_URI'] = "/test/"){ $url = $_SERVER['HTTP_HOST']; $listDomain = array( "k.example.com"=>"В k", "s.example.com"=>"В s", "v.example.com"=>"В v", "t.example.com"=>"В t", "b.example.com"=>"В b" ); foreach ($listDomain as $domain => $title) { if ($url == $domain) { $APPLICATION->SetPageProperty("title", $title); $APPLICATION->SetPageProperty("description", $title); $APPLICATION->SetPageProperty("keywords", $title); } } } 

Option with function. $_SERVER is a superglobal array, there is no need to pass it to a function. But $APPLICATION needs to be passed or declared global global $APPLICATION inside the function for the function to see it (this works, taking into account that bitrix $ APPLICATION is already set as global).

 if ($_SERVER['REQUEST_URI'] = "/test/") { function seoDomain() { global $APPLICATION; $currentHost = $_SERVER['HTTP_HOST']; $listDomain = array( "k.example.com"=>"В k", "s.example.com"=>"В s", "v.example.com"=>"В v", "t.example.com"=>"В t", "b.example.com"=>"В b" ); foreach ($listDomain as $domain => $title) { if ($currentHost == $domain) { $APPLICATION->SetPageProperty("title", $title); $APPLICATION->SetPageProperty("description", $title); $APPLICATION->SetPageProperty("keywords", $title); } } } seoDomain(); } 
  • Thank you for this solution, but it’s still interesting how the function will still look like? - ChromeChrome 7:44 pm
  • @ChromeChrome added option with function .. - jekaby