What are some best practices for handling URL rewriting in PHP to avoid conflicts with file paths and resources?

When handling URL rewriting in PHP, it is important to avoid conflicts with file paths and resources by using a combination of regular expressions and conditionals to ensure that the rewritten URLs do not interfere with existing files or directories on the server.

<?php
// Get the requested URL
$request_uri = $_SERVER['REQUEST_URI'];

// Check if the requested URL does not point to an existing file or directory
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $request_uri)) {
    // Perform URL rewriting logic here
    // For example, you can use mod_rewrite rules in your .htaccess file
    // RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]
}
?>