Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force HTTPS on .htaccess but only on production

I have been looking around for a snippet of code that will force https://. I've found this:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The problem is when I want to work on local so a url like "localhost" or "example.dev" it tries to redirect to https://localhost (it doesn't work).

How do I solve this?

(so to sum it all up, I want the snippet above to work only when I use it on production, e.g "example.com")

like image 387
viralpickaxe Avatar asked Sep 12 '14 23:09

viralpickaxe


1 Answers

I want the snippet above to work only when I use it on production, e.g example.com)

You can restrict your rule to only target example.com using a rewrite condition:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
like image 182
anubhava Avatar answered Sep 30 '22 14:09

anubhava