Force HTTPS on .htaccess but production only
I was looking for a piece of code that will force https://
. I found this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The problem is when I want to work locally, so a url like "localhost" or "example.dev" tries to redirect to https://localhost
(it doesn't work).
How to solve this?
(to sum it all up, I want the snippet above to only work when I use it when building eg example.com).
source to share
I want the snippet above to only work when I use it when creating, for example
example.com
)
You can restrict your rule to targeting only example.com
with 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]
source to share
This is somewhat advanced, but you can try to put your servers in a file called servers.txt
RewriteEngine on
RewriteMap lb rnd:servers.txt
RewriteRule ^/(.*) http://${lb:servers}/$1 [P,L]
serverlist.txt will contain a list of the servers:
## serverlist.txt file con
servers localhost.example.com|server1.example.com
The above answer is good enough with local and production differences in mod rewrite behavior. I would .htaccess in git or subversion (which you ever use) in such a case.
This is mainly for load balancing, but you can apply this in your engine.
source to share