Search code examples
http-redirecthttpscnamenamecheap

Why are my URL redirects not working without www?


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?


Solution

  • 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