1

I've recently moved a simple PHP site to Elastic Beanstalk but am finding it difficult to redirect the www.example.com to example.com. It's not https which most of the suggestions I have found are related to.

This is what I have:

Route53:

A      example.com      example.eu-west-1.elasticbeanstalk.com (Alias)
CNAME  www.example.com  example.eu-west-1.elasticbeanstalk.com

And in my .htaccess file I have the following:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>

When visiting example.com everything is fine but visiting www.example.com gives me a redirect loop:

http://www.example.com/var/www/html/://example.com/var/www/html/://example.com

Is this the best way to redirect on AWS?

EDIT

I have now also tried

RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{SERVER_NAME} ^(www\.)?(.*)$ [NC]
RewriteRule ^/?(.*)$ http://%2/$1 [L,R=301]

and while www.example.com does redirect to example.com, it now has the redirect loop (althought without the server path in the address bar).

2
  • What is this %{ENV:PROTO} for? It does not look appropriate here. Apr 3, 2016 at 2:03
  • @MichaelHampton It's straight out of the HTML5 Boilerplate .htaccess file which worked fine for most of my projects
    – Craig
    Apr 3, 2016 at 2:20

1 Answer 1

1

This is not a solution to your .htaccess problem, but an easy redirect solution is to redirect using an S3 bucket.

  1. Setup an S3 bucket for www.example.com and set it up for website redirection to example.com
  2. Set the record set for www.example.com in Route 53 as an ALIAS for this bucket.

This way, when your user goes to www.example,com, they will hit the bucket and that will redirect them to example.com, which will then go to your server.

8
  • Hmm... this is an interesting solution. Do you know if there are any performance implications of doing it this way? I've voted this up as it certainly works.
    – Craig
    Apr 3, 2016 at 2:21
  • Amazon S3 is pretty fast and designed to scale. Many static websites use S3 exclusively and I use it for production redirection (from example.com to www.example.com). I am not aware of any performance concerns of using it. If performance is a major concern from all parts of the world, you could front this bucket with CloudFront which would cache the results for faster redirects. Apr 3, 2016 at 2:35
  • Also, it lessens the load on your own server since your server will never see the redirect requests. Apr 3, 2016 at 2:36
  • It seems pretty fast and I use the same redirection for static sites so it will work just fine. This seems like the best solution.
    – Craig
    Apr 3, 2016 at 12:04
  • This just adds latency to the requests. Generally adding another service between what you already have will impact performance.
    – Victor
    Mar 28, 2018 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .