I am trying to connect all links to files in a private directory to use SSL.

This is what I have done so far;

if (preg_match('!^system/files/webform/([^/]+)(/.*)?!', $path, $matches)) {

    if($_SERVER['SERVER_PORT'] != '443') { 
       header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); 


This forces private directory file links to use https but it has some issues for users who are not logged into the site.


Depending on where you've placed the above code (e.g. a hook_init invocation), it might not trigger for an anonymous user if caching is involved on your site.

An alternative to this approach is to do it at the web server level so that any request that matches a URL pattern will always switch over to HTTPS. For example, with Apache RewriteRule you could do the following:

  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^system/files/webform/(.*)$ https://www.example.com/system/files/webform/$1 [R=301,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.