{"id":2658,"date":"2022-04-22T18:16:33","date_gmt":"2022-04-22T10:16:33","guid":{"rendered":"https:\/\/www.intelliwolf.com\/?p=2658"},"modified":"2022-04-22T18:16:36","modified_gmt":"2022-04-22T10:16:36","slug":"redirect-http-to-https-htaccess","status":"publish","type":"post","link":"https:\/\/www.intelliwolf.com\/redirect-http-to-https-htaccess\/","title":{"rendered":"How To Redirect HTTP To HTTPS With htaccess"},"content":{"rendered":"\n

On a site I run, we were occasionally having issues with the way plugins were redirecting HTTP requests to HTTPS.<\/p>\n\n\n\n

We were trying to get the most speed out of the site and they were causing load on the database checking whether they should redirect.<\/p>\n\n\n\n

We only had cPanel access, so I did it through .htaccess.<\/p>\n\n\n\n

Pretty much every website now has an SSL certificate. There's no reason to do https to http redirects, so I've just covered the main scenarios you'll come across below.<\/p>\n\n\n\n

If these don't work and you're using an Apache setup (if you're not sure, you're probably using Apache), you may not have the Apache mod_rewrite module<\/a> installed and active.<\/p>\n\n\n\n

How to redirect http to https in .htaccess<\/h2>\n\n\n\n

Edit the .htaccess file in cPanel<\/a> or through FTP<\/a>, edit the .htaccess file in the public_html folder. Add this code to the top:<\/p>\n\n\n\n

RewriteEngine On\nRewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]<\/code><\/pre>\n\n\n\n

You can paste that directly, without changing anything.<\/p>\n\n\n\n

If you don't see a file called .htaccess<\/em>, you may need to turn \"Show hidden files\" on<\/a>.<\/p>\n\n\n\n

That will cause http:\/\/yourdomain.com to redirect to https:\/\/yourdomain.com and http:\/\/www.yourdomaincom to redirect to https:\/\/www.yourdomain.com. Any other subdomains will redirect similarly.<\/p>\n\n\n\n

How this works<\/h3>\n\n\n\n

RewriteEngine On<\/em> tells the server to process rewrite rules. You only need to specify it once in your .htaccess, before you use any rewrite rules.<\/p>\n\n\n\n

Given you're putting this at the top of the document, you should leave it in here. Specifying it multiple times in .htaccess won't hurt your site.<\/p>\n\n\n\n

RewriteCond %{HTTPS} off<\/em> checks whether the request contains a https check. If it's off, it will trigger the rewrite rule below it. Think of RewriteCond<\/em> as \"if\".<\/p>\n\n\n\n

In some old code, you may have seen this line as RewriteCond %{SERVER_PORT} 80<\/em>. That will often still work, but what if the server is configured differently to the usual? You want to know if the request contains https, so you should just check for that.<\/p>\n\n\n\n

RewriteRule<\/em> tells the browser to process the rest of the line if the condition above is satisfied.<\/p>\n\n\n\n

^(.*)$<\/em> is a regular expression<\/a> (also called regex) that means:<\/p>\n\n\n\n