-1

In my AWS Route 53 control panel I simply have 2 A records currently set up for the 'www' and the 'non www' names. Both point to the Elastic IP address associated with my EC2 Instance. This works well and my website is available at both variations but I really want all 'www' to route to the 'non www'.

What is the reccomened method, using AWS Route 53, for routing all traffic that comes to...

www.example.com

to

example.com

2 Answers2

1

It is not the function of DNS to rewrite your web application's URLs. You do this in the web server or in the web application itself.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
1

DNS can't force a redirect. Even a CNAME would just resolve to the same IP address, not cause the browser to redirect to a new URL.

If you're running apache, you can do this using mod_rewrite:

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

The above checks to see if you're hitting www.example.com and if so, redirects to just example.com with a 301 permanent redirect.

You could do something similar that forced any hostname they've come in with that isn't 'example.com' to be redirected:

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

With that, anything that doesn't match exactly "example.com" would be redirected to example.com.

Jason Floyd
  • 1,792
  • 1
  • 13
  • 18
  • Isn't a redirect in the httpd.conf file a less heavy approach then a full mode_rewrite? – Dan Christian Sep 18 '12 at 20:29
  • mod_rewrite has great performance. There's a big perf hit if you're using .htaccess files though. I would put the above code in your httpd.conf or vhosts config file, not in an .htaccess files. .htaccess files are read every request, but the main httpd.conf and it's includes are only read at startup. – Jason Floyd Sep 28 '12 at 22:11