I've been trying to get Drupal to work with SSL (mod_ssl/Openssl),
virtualhost, and Apache webserver. Before go on, let me give you
the specifications of each involving component:

Drupal: 4.6.5
OS: Slackware 10.1
Kernel: 2.4.14
OpenSSL: 0.9.7d
Apache: 2.0.52
mod_ssl: 2.8.22

One last thing, I am utilizing clean URL with drupal and it has been
working great with the normal settings for a while (non-SSL that is).
My initial plan/goal was to use SSL for certain number of pages within
Drupal, mainly login (user/admin) and account sensitive information.
I was planning to accomplish that with help of RewriteEngine to
redirect from http://drupalsite/user to https://drupalsite/user. But
while I was trying to get the rewrite to work, I realize even if I
directly point my browser to any https://drupalsite/... (or even
drupal's root directory, that is https://drupalsite/), the page is not
being encrypted in any form (no sign of lockbox in the browsers, IE,
FF, Mozilla, etc.). So for now, I am struggling to get the encryption
to work with drupal and taken out all redirections out, except drupal's
default rewrite so I can debug the SSL issue first.

The SSL works fine with everything else except drupal. Case in point,
I created a separate virtualhost with different docroot and tried
a dummy FORM with user/pass fields, ran the packet sniffer to see
if the POST is being encrypted which it did. Then I assumed there
must be something wrong with drupal's virtualhost settings. So I
placed the same form from that virtualhost in the drupal's docroot
(that is http://drupalsite/form.html); Then I tried passing user/pass
with https of that URI (https://drupalsite/form.html) and it DID
encrypted the information (I verified this via the packet sniffer
versus plain http of the same page).

So I am to believe drupal internal work is the culprit because the
same SSL settings for different virtualhosts AND even static pages
in drupal's virtualhosts do utilize SSL but not with drupal itself.
I am using the default redirection .htaccess provided by drupal, all
directly in my /path/to/apache2/conf/httpd.conf.

Another thing to point is the especial characteristic of doing
rewrite rules with SSL. When I swith to https://drupalsite/, the
page does not get encrypted but "https" remains on the address bar
as the protocol. To force the entire site into SSL:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]

Or, in a .htaccess or block, you'd need to remove that
leading slash:

RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]

But then again, I have my .htaccess included in conf/httpd.conf and
not using any . I did test conf/drupal.conf with the
added backslash:

# conf/drupal.conf
RewriteRule ^/(.*)$ /index.php?q=$1 [L,QSA]

And it still refused to work -- the page just does not want to get
encrypted.

I have tried to play around with drupal/sites/default/settings.php's
base_url to switch back and forth between "http" and "https" with
or w/o conditions and what not -- it didn't work:

if($_SERVER["SERVER_PORT"] == 443){
  $base_url = 'https://drupalsite';
} else {
  $base_url = 'http://drupalsite';
}

I even took out "http" from base_url to see if drupal can automatically
handle various protocols, no luck. I know name-based virtualhost does
not work with SSL and I am soley running one virtualhost with it (port
443). I've included my virtualhosts of http and https for drupal and
currently, drupal's http works as before but https just does not encrypt
any page. I thought maybe there is some internal iframe with https link
being placed but that was not the case as the packet sniffer showed the
unencrypted information being passed to the server.

I even comment out drupal's default rewrite file from conf/httpd.conf
to see whether it would work with non-clean URL... Still the same result.
I should mention that in all testing conditions, when a new browser is
opened up and https is requested, I do get a typical SSL handshake/
negotiation/server certificate exchange (as the packet sniffer indicates).

I have looked around drupal's forums but haven't found an answer to
my problem. I know several people have gotten it to work so if someone
can shed a light here, I would greatly appreciated.

# conf/drupal.conf
# Apache/PHP/Drupal settings:
#

# Protect files and directories from prying eyes.
<FilesMatch "(\.(engine|inc|module|sh|sql|theme|tpl|xtmpl)|code-style\.pl|Entries.*|Repository|Root)$">
  Order deny,allow
  Deny from all
</FilesMatch>

# Set some options.
Options -Indexes
Options +FollowSymLinks

# Customized error messages.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php

# Override PHP settings. More exist in sites/default/settings.php, but
# the following cannot be changed at runtime. The first IfModule is
# for Apache 1.3, the second for Apache 2.
<IfModule mod_php4.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
</IfModule>

<IfModule sapi_apache2.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
</IfModule>

# Reduce the time dynamically generated pages are cache-able.
<IfModule mod_expires.c>
  ExpiresByType text/html A1
</IfModule>

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # Modify the RewriteBase if you are using Drupal in a subdirectory and
  # the rewrite rules are not working properly.
  #RewriteBase /drupal

  # Rewrite old-style URLs of the form 'node.php?id=x'.
  #RewriteCond %{REQUEST_FILENAME} !-f
  #RewriteCond %{REQUEST_FILENAME} !-d
  #RewriteCond %{QUERY_STRING} ^id=([^&]+)$
  #RewriteRule node.php index.php?q=node/view/%1 [L]

  # Rewrite old-style URLs of the form 'module.php?mod=x'.
  #RewriteCond %{REQUEST_FILENAME} !-f
  #RewriteCond %{REQUEST_FILENAME} !-d
  #RewriteCond %{QUERY_STRING} ^mod=([^&]+)$
  #RewriteRule module.php index.php?q=%1 [L]

  # Rewrite current-style URLs of the form 'index.php?q=x'.
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]

  #RewriteCond %{REQUEST_FILENAME} !-f
  #RewriteCond %{REQUEST_FILENAME} !-d
  #RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>





#
# conf/httpd.conf
#

<VirtualHost 192.168.1.126:443>
    ServerName drupal.domain.com:443
    DocumentRoot /var/www/drupal
    ServerAdmin stop@spamming.damn.it

    # Per-Server Logging:
    # The home of a custom SSL log file. Use this when you want a
    # compact non-error SSL logfile on a virtual host basis.
    CustomLog logs/ssl_access_drupal.domain.com_log \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
    ErrorLog logs/ssl_error_drupal.domain.com_log


    # Enable/Disable SSL for this virtual host.
    SSLEngine on
    # Here I am allowing SSLv3 and TLSv1, I am NOT allowing the old SSLv2.
    SSLProtocol all -SSLv2


    # SSL Cipher Suite:
    SSLCipherSuite HIGH:MEDIUM

    # Server Certificate:
    SSLCertificateFile /path/to/drupal.domain.com.crt

    # Server Private Key:
    SSLCertificateKeyFile /path/to/drupal.domain.com.key

    # Server Certificate Chain:
    SSLCertificateChainFile /path/to/ca.domain.com.crt

    # Certificate Authority (CA):
    SSLCACertificateFile /path/to/ca.domain.com.crt

    SetEnvIf User-Agent ".*MSIE.*" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0

    # I have all the default .htaccess rewrite rules in conf/drupla.conf and
    # Using clear URL.
    Include conf/drupal.conf

</VirtualHost>


NameVirtualHost 192.168.1.126:80

<VirtualHost 192.168.1.126:80>
     DocumentRoot /var/www/drupal
     ServerName drupal.domain.com
     ServerAdmin stop@spamming.damn.it

     # Just like the SSL enabled virtualhost, I am using the default rewrite
     # engine.
     Include conf/drupal.conf

</VirtualHost>

Comments

oxblood’s picture

Very strange, all of a sudden everything is working. I went through everything
to see if I had modified something and hadn't noticed. Apparently, upon many
combination of try-outs, this one does the job:

As I had mentioned, SSL and rewrite are not too friendly so according to this
page ( http://rewrite.drbacchus.com/rewritewiki/SSL ), we need to make this
change to our drupal.conf rewrite rule:

# conf/drupal.conf
RewriteRule ^/(.*)$ /index.php?q=$1 [L,QSA]

Note before we had ^(.*)$ but you need to add "/" to the beginning
of it -- ^/(.*)$. In addition, add the following base_url condition setter
to drupal/sites/default/settings.php:

if($_SERVER["SERVER_PORT"] == 443){
  $base_url = 'https://drupalsite';
} else {
  $base_url = 'http://drupalsite';
}

If you are running your drupal https site with the different port, change it
to the designated port in it and also your SSL-enabled virtualhost. Now I can
tackle redirection of users from http to https when they access certain
pages. Ok, the only annoyance I have is that every page I load with https,
I am asked whether I want the display nonsecure items on the page. Answering
NO would keep your page data sent encrypted which is not a big deal for
me now and I'll elaborate on the workarounds along with redirection of http
to https in the future replies to this post.

jasonwhat’s picture

I'm mostly writing to bookmark this page for myself, but I do think there are some good ssl instructions with the authorize.net module which is in contrib of ecommerce.

adolg’s picture

Well, in my case everything worked on Drupal 4.6 without any changes, but I have "nonsecure items" message boxes as well. What exactly causes them? All links are relative, base starts with https://. I have 3 page templates only 1 on them doesn't produce that message:
https://www.londonaquarium.co.uk (template 1)
https://www.londonaquarium.co.uk/node/700 (template 2)
https://www.londonaquarium.co.uk/image (template 3 - no message)
I have them in IE only, FireFox doesn't bother me with those messages.

Also, I would like https to be used only with one module now, but once one https:// URL is used, all the links in the menu and so on become https://, as they are all relative, so I wonder how to steer it back to http? using mod_rewrite perhaps?

robert.redl@easytouch.cc’s picture

Maybe it has to do with the iframe of drupal.js (if you have upload.module) enabled.

See this issue http://drupal.org/node/71004

alynner’s picture

I have tried to use SSL like you have said above, but it seems that no matter what I do, it just returns a 404 not found error. It does ask you to accept the certificate first though.

Is there something else I am supposed to do before all this?

gcassie’s picture

Try increasing php's maximum memory. I bumped mine up to 12M and it started working. Now if I could just get it to work with clean urls...

elgreg’s picture

I know it was a long time ago - but I'm experiencing the same thing now with 404 errors and authorize.net - any luck on this?

-- Figured this 404 thing out - I'm using subdomains on my setup and had to get the virtual hosts to point to the right place. I just edited the ssl.conf to point straight to the subdirectory that I was developing on.

(you can figure out where it's point to by default by going to https://localhost/)

For setting up on my MAMP install I used:
http://www.macosxhints.com/article.php?story=20041129143420344

and it did the job really well.

Dan C.’s picture

I am trying to set up an ssl using the shared ssl provided by hostgator.

The way to use the secure server layer recommended by hostgator is like so: https://ns123.hostgator.com/~AccountName/SubFolder/
AccountName is the name registered with hostgator and represents the main domain name.
SubFolder is basically a deferent domain name used under the same main account.

With hostgator you can host unlimited domains under one main account.

When I am using the main account https://ns123.hostgator.com/~AccountName/ all the files on the main account and the files within folders of the main account can be accessed using the ssl.
When I am using the SubFolder under the main account https://ns123.hostgator.com/~AccountName/SubFolder/ I can use the ssl - no problem.
Only when I am trying to use files within the SubFolder I’ll get an error page.

This problem occurs only when .htaccess files with RewriteRule is used in the SubFolder.
When the .htaccess file is not in use or more accurately no RewriteRule within the .htaaccess file (using only plain html files) – there is no problem to use the ssl under SubFolders and all files within the SubFolders.

I have tried to add or change RewriteRule, RewriteBase under. Htaccess - no luck.
I have tried to add or change $base_url in the settings.php file - no luck.

I have also tried following the suggestion at: http://drupal.org/node/61099 - no luck.

Any suggestion on the correct setup for .htaccess and settings.php to solve this problem?

loyukfai’s picture

mwu’s picture

I have a lot of questions and will have to work through this thread.

I've never set up an site with SSL.

can anyone tell me how long it takes? I thought it's something that would take 3 hours if I know what I'm doing, but the people here seem pretty knowledgeable and still seems to be taking a long time

are you using Verisign or someone else? I posted a question about recommended certificate authorities here
http://drupal.org/node/80040