2

I'm running an app in Elastic Beanstalk using Tomcat. I set up my own domain name that I've added as a CNAME alias of the xyz.elasticbeanstalk.com address for my environment and it all works well. What I'd like to do is redirect traffic accessing the site at xyz.elasticbeanstalk.com to my own domain name.

I thought I could do this using .ebextensions in my WAR file like this:

myapp.config:

container_commands:
    01_setup_rewrite:
        command: "cp .ebextensions/redirect_canonical.conf /etc/httpd/conf.d/redirect_canonical.conf"
    02_apache_restart:
        command: /etc/init.d/httpd restart

redirect_canonical.conf:

RewriteEngine On
RewriteCond %{HTTP_HOST}   !^www\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/?(.*)         http://www.mydomain.com/$1 [L,R,NE]

I've used similar mod_rewrite rules before in a .htaccess file and they worked well but because this is a mod_proxy I'm not sure if this is right (or if I can even put the rewrite command in the conf.d folder).

How can I configure it do redirect as I would like?

Steffen Opel
  • 63,899
  • 11
  • 192
  • 211
Stephen
  • 161
  • 5

1 Answers1

0

From Mason G. Zhwiti's answer to the related question How to get Elastic Beanstalk nginx-backed proxy server to auto-redirect from HTTP to HTTPS? I'd figure that you need to approach this in a similar fashion for Apache, e.g. something like this (Bash script omitted due to lack of time for actually testing it):

files:
  "/tmp/45_apache_redirect_canonical.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #! /bin/bash
      # TODO: create script that injects content of your 'redirect_canonical.conf' 
      #       into (presumably) '/etc/httpd/conf.d/00_elastic_beanstalk_proxy.conf'

container_commands:
  00_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_apache_redirect_canonical.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
  01_configdeploy_rewrite_hook:
    command: cp -v /tmp/45_apache_redirect_canonical.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
  02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_apache_redirect_canonical.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_apache_redirect_canonical.sh
  03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_apache_redirect_canonical.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_apache_redirect_canonical.sh

Good luck!

Community
  • 1
  • 1
Steffen Opel
  • 63,899
  • 11
  • 192
  • 211