Organizational Research By

Surprising Reserch Topic

How to force a dedicated SSL using the .htaccess file



asked May 20, 2015 in htaccess by rahulgii
0 votes
26 views



Related Hot Questions

3 Answers

0 votes

Important:If you have existing code in your .htacess, add this above where there are already rules with a similar starting prefix.

 

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

Be sure to replace www.yourdomain.com with your actual domain name.

To force a specific domain to use HTTPS, use the following lines of code in the .htaccess file in your website's root folder:

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

Make sure to replace example\.com with the domain name you're trying force to https. Additionally, you need to replace www.yourdomain.com with your actual domain name.

If you want to force SSL on a specific folder you can insert the code below into a .htaccess file placed in that specific folder:

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

Make sure you change the folder reference to the actual folder name. Then be sure to replace www.yourdomain.com/folder with your actual domain name and folder you want to force the SSL on.

answered May 20, 2015 by rahulgii
0 votes

There are two ways to force SSL on a specific file. If using a php file, you can add code to the beginning of that particular file as below:

if (!isset($_SERVER['HTTPS']) || !$_SERVER['HTTPS']) { // if request is not secure, redirect to secure url
$url = 'https://' . $_SERVER['HTTP_HOST']
. $_SERVER['REQUEST_URI'];

header('Location: ' . $url);
exit;
}



Or you can use code in the .htaccess file that specifically addresses that file:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder/file.html
RewriteRule ^(.*)$ https://www.example.com/folder/file.html$1 [R,L]



The second is likely the easiest, especially if you are using html files and do not have it set to run php code.
Either of these should cause that specific file to run the https protocol.

answered May 20, 2015 by rahulgii
0 votes

You may want to try and remove the following 3 lines from your WordPress section and place them above it.:
RewriteCond %{SERVER_PORT} 443 
RewriteRule ^(.*)$ http://www.boostyoureco.org/$1 [R,L]
RewriteRule ^index\.php$ - [L]

Next, take the rewrite rules and combine them together like so:
RewriteRule ^(.*)$ http://www.boostyoureco.org/index\.php [R,L]

So before the Wordpress section, you should have the following lines:
RewriteCond %{SERVER_PORT} 443 
RewriteRule ^(.*)$ http://www.boostyoureco.org/index\.php [R,L]

That should allow your site to process the link and force the standard version for anything coming in on the https port (443).

answered May 20, 2015 by rahulgii

...