How can regex be effectively used to create "CleanURLs" for user readability in PHP?
To create "CleanURLs" for user readability in PHP, we can use regex to rewrite URLs that include query parameters into a more user-friendly format. This can help improve the user experience and make URLs easier to understand and remember. By using regex to extract relevant information from the URL and then rewriting it in a cleaner format, we can create URLs that are more SEO-friendly and user-friendly.
// Rewrite URLs with query parameters into clean URLs
if (preg_match('/^\/([a-zA-Z0-9-]+)\/([0-9]+)$/', $_SERVER['REQUEST_URI'], $matches)) {
$page = $matches[1];
$id = $matches[2];
// Use $page and $id to fetch content from database or perform other actions
// Example: include('pages/' . $page . '.php');
} else {
// Handle other URLs or show a 404 error
header("HTTP/1.0 404 Not Found");
echo "404 Not Found";
}