How can I redirect requests to http(s)//domain.tld/WHATEVER.php
to http(s)//domain.tld/WHATEVER.php?lang=<?php substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); ?>
in PHP?
So, if you visit a URL already with a lang parameter, fine, do nothing. If the lang parameter is not present, 301 redirect to the URL containing the lang param.
Since lang is a GET variable, you can simply check if it is set. If so, redirect to the desired URL.
if(!isset($_GET['lang'])){
header('location: ' . 'http(s)//domain.tld/WHATEVER.php?lang='. substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
exit;
}