Securing Drupal’s user login

Now that I managed to get SSL set up using a wildcard certificate (*.kkaefer.com) signed by CACert, securing the login to a Drupal site is amazingly easy:

# Force the user to use https.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule /(user|admin) https://%{HTTP_HOST}%{REQUEST_URI} [R]

# Enfore SSL for user login and administration.
<LocationMatch "/(user|admin)">
    SSLRequireSSL
</LocationMatch>

That’s pretty much all you need. It works transparently for the user: When the user tries to access a URL beginning with admin or user, a redirect to the exact same request, but with http replaced by https is performed. Additionally, SSL is set to be required for those locations (I added that to prevent logins without SSL in case mod_rewrite fails, becomes unavailable etc.). You can easily add new paths that require SSL by just adding them to both regular expressions.

Sometimes users can login using a URL that doesn’t begin with user. Therefore you should also install securelogin.module which rewrites the actions of forms to use https.

Comments

1
Rajesh Akkineni on August 7, 2008

Thank you for the tip. It saved more than an hour for me.

2
Rajesh Akkineni on August 7, 2008

Where should I keep this rewrite code? I already have default .htaccess file and cleanURLs is enabled.

in .htaccess file I could not find the proper place to put this code. Can you suggest me?

Rajesh Akkineni

3
Konstantin on August 7, 2008

This can be either added to the beginning of the .htaccess file or the virtual server configuration in Apache’s configuration file.

4
don on August 14, 2008

Thanks for the info… We are getting ready to start developing in Drupal but we have been researching the differences between Drupal an WordPress… Can anyone provide any feedback between the pros and cons of each? Thanks!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <div> <span> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h4> <h5> <h6> <img> <br>
  • You can use Markdown syntax to format and style the text.
  • Web page addresses and e-mail addresses turn into links automatically.