Ashley Broadley
Blog Portfolio Pictures
Force SSL using Zend Framework and Apache
My main website, LS12Style.co.uk, has recently been undergoing a facelift and a complete change of direction. At the moment it is meant to act as a portfolio for myself, but since I have started a small Web Hosting business I thought I may as well put it to some good use.
I decided that the control panel that my clients will have access to will use SSL for all communications. Since I have never experienced use of SSL before, I decided to do a little experimentation. I use Apache for my webserver and the Zend Framework to base my applications on.
The Zend Framework is fantastic when it comes to developing and it even has Zend_Tool to get you started. Zend_Tool sets up a basic application structure and all the necessary files in place. It even creates a .htaccess file for you so it can route requests properly, giving you search engine friendly URL's.
Now because I had set up Apache with a popular web hosting control panel, I had to use it to set up the SSL for one of my virtual hosts (my client area for my new website). I googled "force website to use SSl in apache" and google seemed to find some very good answers. Although I did have a bit of trouble getting any of them to work with the already written .htaccess RewriteRules created by the Zend Framework. My first attempt, found here, looked something like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
After many tests with a faux website, it failed. So on my search continued, trawling through Google once again. After a (little, and I mean another 2 minutes) while I came across this solution:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
I modified mine to contain an Apache environment variable, SERVER_NAME, in place of the domain.com, which seems to work.
And hey presto, it worked first time. My thanks go out to the author and the Apache Software Foundation.
Posted on Wednesday 23rd September, 2009 08:42