PHP www a no www redirección

$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

if (preg_match('/www./', $url)){

  $url = str_replace('www.','https://', $url);

  header( "HTTP/1.1 301 Moved Permanently" );
  header( 'Location: ' . $url );
  exit;
}
mvmrik