How To Force HTTPS Using .htaccess

At times you may need to force https across your site or in just certain sections. The recommended way to do this is with a 301 (permanent) redirect.


You can add the following code to your existsing .htaccess, or if you don't have a .htaccess present than go ahead and create one.


```
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
```


Now let's say you have a website in your domains root directory that you do not want to force https, but you have another section of the site in /other that you want to force https on. To do this would look like this:



RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^/?subfolder/(.*) https://%www.yourdomain.com/other/$1 [R,L]


For a specific page it would look like:



RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^example\.html$ https://www.yourdomain.com/sample.html [R,L]


As always if you have any issues implementing these, or have questions don't hesitate to contact our support!



  • https, .htaccess
  • 0 användare blev hjälpta av detta svar
Hjälpte svaret dig?

Relaterade artiklar

Enabling Mysql Remote Access

Opening up Mysql to remote connections is a security risk and so we grant remote access only by...

Using Cron Jobs

Using cPanel Cron JobsCron jobs simply are bash commands put on a schedule. So when looking to...

PHP Selector Supported Versions

We currently offer PHP 5.6, 5.7, 7.0, and 7.1 VIA the PHP Selector in cpanel. All earlier...

Addon Domain | Sorry, the domain is already pointed to an IP address

If when trying to add a addon domain via cPanel you receive an error similar to this:Sorry, the...

Getting Rid Of Sub-domain From Addon Domain

When creating an addon domain in cPanel a few different entry points are created to that addon...