Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Force Secure (SSL) Pages With .htaccess

7 Responses »

A while back, I shared a method for forcing a secure page using PHP. What if you want to force SSL (https://) on an entire website though? You don't want to have to put force-SSL PHP code on every page, right? Well, the website's .htaccess file comes to the rescue.

The .htaccess Code

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

Obviously, you'll want to change "domain.com" to your domain. Another short snippet of code that has a big impact on your website!

Discussion

  1. May 9, 2008 @ 12:43 pm

    Great tip but what if you have more than one domain to the same website? For example, you have davidwalsh.name now, but you could have had the domain davidwalsh.com too. How would you change the script so that it worked on both of your domains?

  2. May 9, 2008 @ 2:15 pm

    If you have a website that uses the www. prefix (the standardized way for top level domains) add www. into the .htaccess code. Otherwise Google will penalize you for duplicate content (your entire site would be one whole duplicate).

    Great tip!

  3. jon
    August 11, 2008 @ 3:12 pm

    Thanks for the venue.
    I’m new to this web admin topic and would like some clarity on what is probably a very simple question for experienced webmasters.

    is it correct that using basic authentication (.htaccess + htpassword) on an Apache server that allows only ssl will not result in clear text being sent from the browser to the server .. since after all it is an ssl connection?

    Also, in general, is htaccess a reasonable way to secure directories given a 100% ssl site?

    Thanks.

  4. February 23, 2009 @ 2:59 pm

    Thanks a lot..
    I was searching this kind of thing….

  5. werner
    June 18, 2009 @ 4:48 am
  6. chris
    September 25, 2009 @ 10:25 am

    Will this script work with subdomains and wildcard SSL certificates?

    yourwebsite.com
    subdomain1.yourwebsite.com
    subdomain2.yourwebsite.com

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!