AWS load balancer non-www to www

amazon ec2amazon-elbamazon-web-services

I want to redirect all traffic non-www to www (without using route53).
I have already created the CNAME www.

How can I do that without changing the code on the site?

The AWS application load balancer is on top of the infrastructure.

Best Answer

This would not be done via DNS or at the load balancer this is achieved at the webserver by issuing a redirect. With apache this is best done on in the .htaccess file in the webroot.

The contents of .htaccess should be as follows:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]