How to Redirect to Force WWW and HTTPS Using .htaccess File

Local Dev Setup

I’ve made a ton of websites over the years, and somethings are done every time.  Redirects in the .htaccess file is one of those things that’s always on the checklist.

Don’t worry about Googling how to do each redirect each time.  Just bookmark this page.

This .htaccess code snippet handles redirecting any requests to…

  • Force the www subdomain if accessed without it
  • Force SSL (https://) if accessed with http://

Put the following in the .htaccess file preferably towards the top, before the WordPress section…

RewriteEngine On
# Force WWW
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [R=302,NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

Of course, replace  with your own domain.

Note: I prefer to use 302 redirects instead of 301 redirects because…

  1. 302s don’t get cached in your browser like 301s.  So, if you mess something up, you can change it and test it without having restart your browser.
  2. I’ve also read that 301s don’t have any more capability of transferring page rank, and 302s actually maintain the transferring page rank more over time.
Notify of

Inline Feedbacks
View all comments