Quick snippet: redirecting requests with a missing www. in Apache
Getting www.
prepended to requests to alexsilcock.net
using mod_rewrite sometimes feels like voodoo, and in the past I've managed to get it partially working but with unexplained infinite redirect loops. To save experimenting with it in the future, here's the easiest way of accomplishing it:
RewriteCond %{HTTP_HOST} !^www\.alexsilcock\.net$ [NC]
RewriteRule . http://www.alexsilcock.net%{REQUEST_URI} [R=301,L]
This works with requests to the base site URL, subdirectories of the site, and is SEO-friendly with its 301 Moved Permanently HTTP response code.