5

I want the following redirect:

http://example.com -> http://www.example.com
example.com -> http://www.example.com
www.example.com -> http://www.example.com

In my namecheap I setup up two URL redirect records:

CNAME Record        | www | appname.us-east-2.elasticbeanstalk.com.
URL Redirect Record |  @  | http://www.example.com                  | Unmasked 

But still, I can only access http://www.example.com when I actually type this full URL. When I remove www then the request times out. So the URL redirects don't seem to do anything. How can I redirect to my full http domain correctly?

user2212461
  • 3,105
  • 8
  • 49
  • 87

2 Answers2

4

You could make www.domain.com the A record and all the other domainnames CNAMEs of www.domain.com. But this only "solves" that if the IP address of www.domain.com changes you don't have to alter the other DNS enties as they are aliases.

So on the DNS level there is no way to enforce a redirect. And for a good reason because DNS is used for more then only HTTP. For example if all request for domain.com would redirect to www.domain.com your email addresses will change to user@www.domain.com.

So for HTTP redirection you will have to use an HTTP solution. This can be at the webserver level (nginx, apache, etc.)

pozix
  • 76
  • 5
3

I use NGINX and I implemented redirection like the following:

vi /etc/nginx/sites-enabled/example-com

server {
        listen 80;
        server_name example.com;
        return 301 https://www.example.com$request_uri;
}

server {
        listen 80;
        server_name www.example.com;
        root /var/www/html;
        index index.html index.htm;
}

Response status code 301 means "Moved Permanently" and is used for permanent URL redirection.

Please do not forget to restart NGINX.

sudo systemctl restart nginx
flandersen
  • 342
  • 3
  • 10