What are some best practices for implementing mobile version redirection in PHP?

When implementing mobile version redirection in PHP, it is important to detect the user agent of the device accessing the website and redirect them to the appropriate mobile version of the site. One common approach is to use the $_SERVER['HTTP_USER_AGENT'] variable to identify mobile devices and then redirect them accordingly.

$user_agent = $_SERVER['HTTP_USER_AGENT'];

if(preg_match('/iphone|android|blackberry|windows phone|opera mini|iemobile/i', $user_agent)) {
    header('Location: http://m.yourwebsite.com');
    exit;
}