What are the potential pitfalls of using custom URL routing in PHP?

One potential pitfall of using custom URL routing in PHP is the risk of creating insecure routes that could expose sensitive information or compromise the application's security. To mitigate this risk, it is important to sanitize and validate user input to prevent malicious attacks such as SQL injection or cross-site scripting.

// Example of sanitizing and validating user input in a custom URL routing function

$route = $_GET['route'] ?? '';

// Sanitize and validate the route parameter
if (preg_match('/^[a-zA-Z0-9_-]+$/', $route)) {
    // Process the route
} else {
    // Redirect to a safe default route
    header('Location: /default-route');
    exit;
}