What role does .htaccess play in managing non-existent pages in PHP websites?

When a non-existent page is accessed on a PHP website, it typically results in a 404 error. To manage non-existent pages and redirect them to a custom error page, you can use the .htaccess file to rewrite URLs and handle the redirection.

RewriteEngine On
ErrorDocument 404 /error.php