Clean URL Routing

Users and search engines prefer readable URLs over query strings. Achieving clean routes doesn’t require a framework; a little Apache configuration and a simple PHP router will do.

1. Rewrite rules

Add rewrite rules to your .htaccess to funnel all requests to a single PHP entry point. Exclude existing files so assets are served normally.

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L]

2. Parsing the path

In your index.php, examine $_SERVER['REQUEST_URI'] and map paths to controllers. Use a switch or array of callbacks for clarity.

$path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); switch ($path) { case 'about': require 'pages/about.php'; break; default: require 'pages/home.php'; break; }

3. Generating links

When building links, point to your clean routes. Avoid exposing underlying filenames in HTML; this keeps your structure flexible.