Categories
AWS General Operating System

Redirect to https and www

The following .htaccess technique redirects qualified requests to the https and www versions of your web pages. Add to your site’s root .htaccess file:

# Canonical https/www

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This code does the following:

Checks if mod_rewrite is available
Check if the request does not include www
Checks if HTTPS is off,

No editing is required with this code; it’s entirely plug-n-play.

Leave a Reply