0

I am using an AWS Elastic Load Balancer, and have set up the following rule to convert http traffic to https.

/etc/httpd/conf.d/httpd_redirect.conf

    <VirtualHost *:80>
        RewriteEngine On
        RewriteCond %{HTTP:X-Forwarded-Proto} !https
        RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    </VirtualHost>

However, when I access my website via its domain (www.thewhozoo.com), I can see the protocol is http and not https.

Any ideas why the rewrite rule is not working?

Thanks

More info

/etc/httpd/conf/httpd.conf

...
Include conf.d/*.conf
Include conf.d/elasticbeanstalk/*.conf
...

The above is created on deployment by:

.ebextensions/myapp.config

container_commands:
      01_setup_apache:
          command: "cp .ebextensions/enable_mod_deflate.conf /etc/httpd/conf.d/enable_mod_deflate.conf"
files:
  "/etc/httpd/conf.d/httpd_redirect.conf" :
    mode: "000644"
    owner: root
    group: root
    content: |
        <VirtualHost *:80>
            RewriteEngine On
            RewriteCond %{HTTP:X-Forwarded-Proto} !https
            RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
            RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
        </VirtualHost>

ELB Listeners

enter image description here

Richard
  • 145
  • 1
  • 10

1 Answers1

2

You have a configuration snippet /etc/httpd/conf.d/ssl_rewrite.conf

Typically such snippets get loaded by an Include or IncludeOptional directive

Include conf.d/*.conf

or

IncludeOptional conf.d/*.conf

from your main httpd.conf.

The problem is that those snippets only apply to your main configuration and don't apply to any VirtualHost sections...

Either include those settings in the virtual host definition, or load that snippet there:

<VirtualHost *:80>
   ServerName www.thewhozoo.com
   Include /etc/httpd/conf.d/ssl_rewrite.conf
</VirtualHost>

Or even better, don't use mod_rewrite and set:

<VirtualHost *:80>
    ServerName www.thewhozoo.com
    Redirect "/" "https://www.thewhozoo.com/"
</VirtualHost>
HBruijn
  • 77,029
  • 24
  • 135
  • 201
  • Thanks for the great answer. If I use `Redirect "/" "https://www.thewhozoo.com/"`, where do I put it? i.e. in which config file? – Richard Jun 26 '17 at 18:43
  • In the config file where you define the virtual host for your domain. That might be in a `sites-available/` or in your `conf.d/` - I don't know. – HBruijn Jun 26 '17 at 18:44
  • Excuse my ignorance, but do I add it to a `.config` file in `.ebextensions`, so that at deploy time it is added to a `.conf` file? e.g. in my example above, add it to the `.ebextensions/myapp.config` file? – Richard Jun 26 '17 at 18:46
  • I'm similarly ignorant, I know Apache httpd, but I don't know what .ebextensions does... :( – HBruijn Jun 26 '17 at 18:51
  • `.ebextensions` is a directory you add to your app root at build time, and when you deploy Tomcat, the commands are executed before the server starts. – Richard Jun 26 '17 at 18:53
  • I added a `virtual_hosts.conf` file with the following, but no success. `$ cat virtual_hosts.conf ServerName www.thewhozoo.com Redirect "/" "https://www.thewhozoo.com/" ` – Richard Jun 26 '17 at 19:26
  • I added a `virtual_hosts.conf` file with the following, but no success. `$ cat virtual_hosts.conf ServerName www.thewhozoo.com Redirect "/" "https://www.thewhozoo.com/" ` – Richard Jun 26 '17 at 19:26
  • The `httpd.con` already has a `Include conf.d/*.conf` in it, so I think the original `ssl_rewrite.conf` is being included, but not sure why it's not working. – Richard Jun 26 '17 at 19:28