Redirecting a naked domain (e.g., example.com) to a domain with 'www' (e.g., www.example.com) in AWS Route 53 is a common requirement. This is primarily because many websites aim to provide services through a unified URL, enhancing brand recognition and improving SEO. To achieve this functionality, follow these steps:
-
Configure DNS Records:
- Create an A Record: First, create an A record (or a CNAME record if using a CDN or other indirect addressing methods) for
www.example.com. This record points to the IP address of your web server or a domain resolving to an IP address. - Alias Record: Since DNS standards prohibit CNAME records for naked domains, Route 53 provides a special record type called an Alias record. Create an Alias record for the naked domain
example.comthat directly points towww.example.com.
- Create an A Record: First, create an A record (or a CNAME record if using a CDN or other indirect addressing methods) for
-
Configure Redirection (if needed): Although Alias records handle DNS queries, they do not handle HTTP redirection. If you want to redirect users from
example.comtowww.example.comat the HTTP layer, configure redirection rules on your web server. Below are examples for common web servers:-
Apache Server: In Apache, add the following rule to the
.htaccessfile:apacheRewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]This code checks if the requested host is
example.comand permanently redirects towww.example.com. -
Nginx Server: In Nginx, add a server block to the configuration file:
nginxserver { server_name example.com; return 301 $scheme://www.example.com$request_uri; }This configuration permanently redirects requests for
example.comtowww.example.com.
-
This covers the basic steps for redirecting a naked domain to www using AWS Route 53. By properly configuring DNS records and web server redirection rules, you can effectively manage traffic and ensure users access the correct website address.