What are the best practices for including CSS, images, and JS files when using RewriteRule in PHP?

When using RewriteRule in PHP to create clean URLs, it's important to ensure that CSS, images, and JS files are still accessible to the browser. One common approach is to check if the requested file exists on the server before applying the RewriteRule. If the file exists, serve it directly; if not, rewrite the URL to the appropriate PHP file.

$requestedFile = $_SERVER['REQUEST_URI'];

if (file_exists($_SERVER['DOCUMENT_ROOT'] . $requestedFile)) {
    return false; // serve the requested file directly
} else {
    // apply RewriteRule to redirect to PHP file
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
}