I want to check each URL as it comes in. If the URL is http, I want to redirect the browser to https.

I have code that I believe will work and I really just need to know what script and/or function I should be inserting it into in order to make this magic work for every single URL that drupal renders.

Also, is this sort of thing possible to do in a module as opposed to hacking the core code?

My code sniplet I am intending to use is:

if (!$_SERVER['HTTPS'])
{
   header('Location: https://www.mysite.com/');
   exit;
}

This code and several other examples are referenced here and here.

Any clues gratefully accepted.

Comments

analogduck’s picture

I got this to work successfully by injecting my code into includes/bootstrap.inc like so (between the tags labeled "// AnalogInsert"):

...
function conf_init() {

// AnalogInsert: Start
// This code checks each page to confirm that no HTTP pages are loaded
// HTTP pages are redirected to https://www.mysite.com/
if (!$_SERVER['HTTPS'])
{
header('Location: https://www.mysite.com/');
exit;
}
// AnalogInsert: End
...

This works, however, I still don't know if it is an optimum hack. Is this the best place to put such code? Can I do this from a module?

-- AnalogDuck
"If at first you don't succeed, chainsaw juggling is probably not for you."

killes@www.drop.org’s picture

I'd set my base_url variable to https://www.mysite.com in conf.php and add a rewrite rule to .htaccess.