title: Forwarding http to https in AWS Load Balancers
body: <p>I'm using Elastic Beanstalk, and I installed my SSL certificate in my EB load balancer. Every time the server is not healthy, the load balancer delete the instance and creates a new one, which means I'm gonna lose my redirection codes and SSL set up that I installed inside the instance.</p>
  
  <p>That's why I installed my SSL on the load balancer. However, how can I redirect HTTP to HTTPS on the load balancer?</p>
  
  <p>I used to redirect by putting the below codes inside the instance, but if I keep doing this way, when the load balancer removes an unhealthy instance, I'm gonna lose my redirection from HTTP to HTTPS.</p>
  
  <p>What should I do?</p>
  
  <pre><code>&lt;VirtualHost _default_:80&gt;
    ServerName (domain).com
    ServerAlias www.(domain).com
    RedirectPermanent / https://www.(domain).com/
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost _default_:443&gt;
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here's my listeners on Load Balancers in EC2</p>
  
  <p><a href="https://i.stack.imgur.com/TSCOb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TSCOb.png" alt="enter image description here"></a></p>