Tomcat – Apache Rewrite rule not working

amazon-web-servicesApache2elastic-beanstalktomcat

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

Best Answer

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>