I have transfered my DNS to Route 53 and currently use an alias record to point my www.domain.com and domain.com to my Elastic Load Balancer.

Both work great but my problem is when I try to use an htaccess file to redirect non-www to www. It just fails DNS lookup and errors out when I visit domain.com and get re-routed to www.domain.com, going straight to www.domain.com works still.

Before I did everything with the load balancer and when I was just testing the instance, I would go to the public DNS for my domain 'ec2-99-99-999-99.compute-1.amazonaws.com' for example and whenever I put the htaccess file in, it would jump to 'www.ec2-99-99-999-99.compute-1.amazonaws.com' and produce an error.

Any help would be much appreciated as I'm tearing my hair out over this.


Without seeing your .htaccess file, it's not entirely clear what's happening, but something like this should work if you have mod_rewrite enabled:

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


If you're seeing a 502 error from the ELB, it's probably because your application isn't returning a full response header and just returning an HTTP response code. There was a discussion about this on the EC2 dev forums: https://forums.aws.amazon.com/message.jspa?messageID=345128

You can do to troubleshoot this by monitoring the network traffic on the server to determine what headers and response codes are being sent back to the ELB. You should probably do this for when it fails (on mobile) and succeeds (on your PC).

Assuming your server is running linux, ngrep is a nice simple tool for this:

# Just show HTTP traffic
ngrep -q '' port 80

# Just show GET and POST requests
ngrep -t '^(GET|POST) 'port 80'

Otherwise you can use Wireshark which runs nicely under windows.

  • My htaccess file works correctly. It's the Alias record in my DNS that is pointing to the Load balancer that is the problem. – user1011713 Mar 18 '13 at 23:09
  • Just to make sure I understand, currently if you enter 'yourdomain.com', it doesn't redirect you to 'www.yourdomain.com' but some other FQDN that doesn't exist? What is the FQDN that the redirect is producing? You can use 'curl -i yourdomain.com' if you're running a unix variant. – seren Mar 18 '13 at 23:19
  • Curl works on both www and non-www. Basically, I have an elastic load balancer and I use amazon's Route 53 DNS system to create an A Host and route both www and non-www domains to the A-Host that is actually my Elastic Load Balancer. Problem is that 1. My DNS complete doesn't work and gives a 502 gateway error when I use a mobile phone but works on a regular PC and 2. If I put an htaccess file on my server's var/www/ folder, it produces an error and doesn't even produce the page. The htaccess file works and there's no error in there since it's worked before. – user1011713 Mar 18 '13 at 23:29
  • Ahh, ok. Just updated my initial answer. – seren Mar 19 '13 at 0:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.