Skip to content

Instantly share code, notes, and snippets.

@webmechanicx
Last active January 23, 2020 18:50
Show Gist options
  • Save webmechanicx/86382970f5a1dbe2ca28787d7d76e740 to your computer and use it in GitHub Desktop.
Save webmechanicx/86382970f5a1dbe2ca28787d7d76e740 to your computer and use it in GitHub Desktop.
How do I redirect HTTP traffic to HTTPS on my Classic Load Balancer in ELB

How do I redirect HTTP traffic to HTTPS on my Classic Load Balancer in ELB?

There are two way to handle Redirect HTTP traffic to HTTPS using Elastic Load Balanacing for AWS:

Implementation-1 (Using Apache Directives)

https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb/#

Command to restart apache httpd:

sudo service httpd restart

Implementation-2 (Using Htaccess)

A base .htaccess with HTTPS redirect behind an AWS Load Balancer

  1. Open your Apache configuration file. Possible locations include sudo nano /etc/httpd/conf/httpd.conf (Apache 2/httpd) or sudo /etc/apache2/sites-enabled/ (Apache 2.4)

  2. Edit the Directory directive to enable .htaccess as follows:

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
  1. Save your Apache configuration file.
  2. Open your .htaccess file.
  3. Add a rewrite rule similar to the following:
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Force HTTPS - Proto needed for AWS ELB
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
    RewriteRule ^.*$ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

</IfModule>

## EXPIRES CACHING ##
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access 1 year"
  ExpiresByType image/jpeg "access 1 year"
  ExpiresByType image/gif "access 1 year"
  ExpiresByType image/png "access 1 year"
  ExpiresByType text/css "access 1 week"
  ExpiresByType text/html "access 1 week"
  ExpiresByType application/pdf "access 1 month"
  ExpiresByType text/x-javascript "access 1 week"
  ExpiresByType application/x-shockwave-flash "access 1 month"
  ExpiresByType image/x-icon "access 1 year"
  ExpiresDefault "access 1 month"
</IfModule>

## EXPIRES CACHING ##
<IfModule mod_deflate.c>
  # Insert filter
  SetOutputFilter DEFLATE
  # Don't compress images
  SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|html)$ no-gzip dont-vary
  # Make sure proxies don't deliver the wrong content
  Header append Vary User-Agent env=!dont-vary
</IfModule>
  1. Save your .htaccess file.
  2. upload using filezilla or any equivalent way
  3. Restart Apache.
sudo service httpd restart

OR

sudo service apache2 start
  1. Finally update CI application/config/config.php by adding:
/**
 * Set dynamic base_url for AWS load balancer
 */
$base_url = (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') 
            ? 'https' : 'http';
$base_url .= "://". @$_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base_url;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment