How do I redirect all 404 error url to Subcategory url

<?php

header("HTTP/1.1 301 Moved Permanently");

header("Location: https://{$_SERVER['SERVER_NAME']}/subcategory{$_SERVER['REQUEST_URI']}"); 

exit();

?>

The above code was added directly to themes 404.php and everything worked as expected

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)