apache modrewrite

useful rewrite rules, options and syntax

useful rules

Force www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Force NON www.
RewriteCond %{HTTP_HOST} ^www\.pulse8\.co\.uk [NC]
RewriteRule ^(.*)$ http://pulse8.co.uk/$1 [R=301,NC]
Remove Trailing Slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
Grab a Particular Variable (Example: changes /product.php?product_id=909 to /product?id=909
RewriteCond %{QUERY_STRING} product_id=(.*)
RewriteRule ^product.php(.*) /product?id=%1