.htaccess redirect to https and www

Category: Blog • Posted by Jeff Starr • Updated:

If your site is serving secure pages via the HTTPS protocol (i.e., via SSL/TLS), you may need a technique to redirect all HTTP requests to HTTPS. Then to go further with your canonicalization efforts, you may also want to redirect all www requests to non-www (or vice versa). Both of these techniques are essential for serving canonical versions of your web pages, so why not combine them into single, simple slice of .htaccess? Read on to learn how..

Redirect to https and non-www

To instead redirect all requests to https and non-www, use the following code instead of the previous:

# Canonical HTTPS/WWW
<IfModule mod_rewrite.c>
	RewriteCond %{HTTPS} off [OR]
	RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
	RewriteRule (.*) https://example.com/$1 [L,R=301]
</IfModule>

As before, place this code in the root .htaccess of your site. Here is what it's doing:

  1. Checks if mod_rewrite is available
  2. Checks if HTTPS is off, or if the request includes www
  3. If either condition matches, the request qualifies and is redirected to the https/non-www address

When placed in the root .htaccess, this technique covers all requests, providing complete https/non-www canonicalization for your site. Remember to replace the two instances of example.com with your own domain name.

Note: if your site is suffering from duplicate pages because of index.php appended to requested URLs, check out this post at WP-Mix.com that explains how to remove www and index.php from the URL.

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
<IfModule mod_rewrite.c>
	RewriteCond %{HTTPS} off [OR]
	RewriteCond %{HTTP_HOST} !^www\. [NC]
	RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
	RewriteRule (.*) https://www.%1/$1 [R=301,L]
</IfModule>

This code does the following:

  1. Checks if mod_rewrite is available
  2. Checks if HTTPS is off, or if the request does not include www
  3. If either condition matches, the request qualifies and is redirected to the https/www address

When placed in the root .htaccess, this technique covers all requests, providing complete https/www canonicalization for your site. No editing is required with this code; it's entirely plug-n-play.

Notes

Depending on how your server is configured, you may need to hard-code the literal name of your domain in the RewriteRule. For example, with the above code in place, try requesting the following URL in your browser:

http://www.example.com/

Notice here we are requesting the non-secure http protocol along with the www subdomain. Of course, you want to replace example.com with the actual name of your domain. Upon making the request, if the server redirects to the following URL, everything is working properly:

https://www.example.com/

Otherwise, if you get something like this with duplicate www subdomains:

https://www.www.example.com/

If you're seeing the double www, you will need to hard-code the literal name of your domain in the RewriteRule of the above technique. So change this line:

RewriteRule (.*) https://www.%1/$1 [R=301,L]

..to this:

RewriteRule (.*) https://www.example.com/$1 [R=301,L]

Remember to change the domain name to match your own.