What are best practices for organizing and structuring mod_rewrite rules in PHP?
When organizing and structuring mod_rewrite rules in PHP, it is important to maintain clarity and readability for easier maintenance and troubleshooting. One best practice is to group related rules together and comment them appropriately to explain their purpose. Additionally, using named capture groups and variables can make the rules more flexible and reusable.
RewriteEngine On
# Redirect non-www to www domain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrite URLs with query parameters
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&id=$2 [L]