Search code examples
apacheamazon-web-servicesdnsamazon-elastic-beanstalkamazon-route53

Redirect Elastic Beanstalk URL to domain name


I have an app hosted on AWS Elastic Beanstalk, which is assigned an environment URL as such:

<my-appname>.<aws-region>.elasticbeanstalk.com

I also have registered a domain name as such:

my-appname.com

In AWS Route 53, I have an A ALIAS pointing my-appname.com to the EB environment as such:

my-appname.com > A ALIAS <my-appname>.<aws-region>.elasticbeanstalk.com

From my registrar, I have Route 53 nameservers set up to manage DNS via Amazon.

Everything Works Fine

What I'd like to understand how to do is ensure any requests to the <my-appname>.<aws-region>.elasticbeanstalk.com> domain get 301'd to the my-appname.com domain.

I'm using an Apache RewriteRule currently to redirect all non-www requests to the www version of the website using this in a .config file:

<If "'%{HTTP_HOST}' !~ /^www\./">
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</If>

Would it be good practice to simply change the HTTP_HOST to my-appname.com?

EDIT: That approach doesn't seem to work anyway. Not sure why?


Solution

  • My current understanding is the best approach for this is to use server-level re-writes to address the issue. An example (for an Apache server) is as follows:

    Rewrite Engine On
    
    # Catch requests to domains other than your primary (custom) domain
    Rewrite Cond %{HTTP_HOST} !~ appname.tld
    
    # Send those requests to the primary domain
    RewriteRule (.*) http://www.appname.tld%{REQUEST_URI} [R=301, L]