What are some common mistakes to avoid when trying to rewrite URLs in PHP for cleaner and more user-friendly URLs?

One common mistake to avoid when rewriting URLs in PHP is not properly handling special characters or spaces in the URL. This can lead to broken links or incorrect routing. To solve this, it's important to use functions like urlencode() to properly encode the URL parameters before using them in the rewritten URL.

// Incorrect way to rewrite URL without handling special characters
$param = $_GET['param'];
echo "<a href='/page/$param'>Link</a>";

// Correct way to rewrite URL with proper encoding
$param = urlencode($_GET['param']);
echo "<a href='/page/$param'>Link</a>";