Skip to content

Instantly share code, notes, and snippets.

@twkm
Last active November 1, 2016 20:07
Show Gist options
  • Save twkm/cc45a5ad6e839e49a2a3b1d06b44774b to your computer and use it in GitHub Desktop.
Save twkm/cc45a5ad6e839e49a2a3b1d06b44774b to your computer and use it in GitHub Desktop.

.htaccess Reference

A collection of useful .htaccess snippets for SEO and development.

Redirect naked domain to www subdomain

# 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>

Redirect to Secure Site

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>

Maintenance redirects

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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment