A collection of useful .htaccess snippets for SEO and development.
# Redirect naked domain to www subdomain
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
This also contains a redirect from the naked domain to the www subdomain. This doesn't affect development environments (.dev domains).
<IfModule mod_rewrite.c>
## Redirect naked domain to www subdomain
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !\.dev$
RewriteRule .*$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
## Redirect non-secure to secure
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !\.dev$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
</IfModule>
Create a maintenance.html page and optionally, maintenance-css/ and maintenance-images/ folders to use for a maintenance site. This redirect will allow you to see pages on the live site from your IP, but all others will receive the maintenance page and assets. Once you're done with maintenance, comment the first three Rewrite lines out and uncomment the last two Rewrite lines. Anyone who was on the maintenance page will be redirected to the home page.
## Maintenance redirects
# Uncomment below if you want to enable a 'maintenance mode'
RewriteCond %{REQUEST_URI} !/maintenance(-images/.*|-css/.*|\.html)
RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.111 # Office IP address
RewriteRule (.*) /maintenance.html [R=307,L]
# Uncomment below once maintenance is complete
#RewriteCond %{REQUEST_URI} /maintenance.html [NC]
#RewriteRule .* / [R=302,L]