Forcing HTTPS and WWW for a website with Apache

Submitted by slava on 18 January 2016

Drupal 7 comes with .htaccess that allows you uncomment a few lines and make the site be always on the "www" or the non-"www" domain. But what if you want to force it to be HTTPS only?

Not a problem! Add these lines to the section that configures the mod_rewrite module in your .htaccess:

RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]

...or these lines if you want the site to be without the "www" part:

RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R=301,L]

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
By submitting this form, you accept the Mollom privacy policy.