Drupal Force SSL – https redirection

SSL secure site redirection can be forced in the following ways

1) Using the virtual hosts file

2) Using .htaccess file

3) Using PHP code in settings.php file

Using virtual hosts file is not possible in a shared hosting environment. If we use .htaccess, during the drupal core update, we have to back up the .htaccess file otherwise it will be overwritten. The third method is using the settings.php file which is not overwritten during the drupal upgrade.

Add the following code at the top of the settings.php file to force the users to redirect to the .https site.

if ((!array_key_exists('HTTPS', $_SERVER)) && (PHP_SAPI !== 'cli') ) {
 if (substr($_SERVER['HTTP_HOST'], 0, 4) <> 'www.') {
 $new_url = 'www.' . $_SERVER['HTTP_HOST']; 
 } else { 
 $new_url = $_SERVER['HTTP_HOST']; 
 }
$new_url .= $_SERVER['REQUEST_URI']; header('HTTP/1.1 301 Moved Permanently'); 
header('Location: https://'. $new_url); exit(); 
}

Please follow and like us:

Recommended Articles

Leave a Reply

Your email address will not be published. Required fields are marked *