generate functionWed, 09 Nov 2022

php if current url is subdomain, redirect to main domain

function remove_subdomain_from_url(){ if(!empty($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] != 'localhost'){ $host = $_SERVER['HTTP_HOST']; if(strpos('www.', $host) === 0){ $host = substr($host, 4); } if(strpos($host, '.') !== false){ $parts = explode('.', $host); if(count($parts) > 2){ unset($parts[0]); $host = implode('.', $parts); header('Location: http://' . $host . $_SERVER['REQUEST_URI']); } } } }

Questions about programming?Chat with your personal AI assistant