.htaccess URL Rewriting

Ugly query strings and index.php in your URLs hinder usability and SEO. Apache’s mod_rewrite module lets you define rewrite rules that map clean, human‑friendly URLs to internal scripts.

Enabling mod_rewrite

Ensure the module is enabled in your Apache configuration. On Debian based systems run:

sudo a2enmod rewrite sudo systemctl reload apache2

Writing rules

Create a .htaccess file in your document root with the following directives:

RewriteEngine On # redirect all requests except existing files RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ index.php?url=$1 [L,QSA]

This rule passes the requested path as a parameter to index.php, where your router can resolve it. Always place RewriteEngine On at the top of the file and test thoroughly.