Below are two samples of what you can use in a .htaccess to force SSL on your PHP site:
#Force SSL on entire site
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://(YOURDOMAIN)/$1 [R,L]
#Force SSL on a specific directory
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^DIRNAME/(.*)$ https://YOURDOMAIN/DIRNAME/$1 [R,L]© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

9 Comments
The above did not work for my subdirectory
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://domain/folder/$1 [R,L]
Re: subdirectory
Did not work for me either
Manuel, your coding did work! Thanks for submitting...Rackspace, please update your article:)
Redirect Loop with SSL
Other way 'round...
#No SSL on entire site
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} =on [NC]
RewriteRule ^(.*)$ https://(YOURDOMAIN)/$1 [R=301,L]
More examples
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^page.php(.*)$ https://example.com/page.php [R,L]
For many more examples of rewrites, Apache's documentation is a good resource.
http://httpd.apache.org/docs/current/rewrite/
Force SSL
#Force SSL on entire site
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://(YOURDOMAIN)/$1 [R,L]
Gives me a "Redirect Loop"
re: redirect loop
Force Non-SSL on Non-Checkout Pages
RewriteEngine On
RewriteBase /
#Force NON-SSL on a non-checkout directories
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !^/CHECKOUT/?.*$
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
#Force SSL on a specific directory
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^CHECKOUT/(.*)$ https://www.example.com/CHECKOUT/$1 [R,L]
Add new comment