Why are my URL redirects not working without www?
Asked Answered
P

2

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?

Palgrave answered 29/4, 2020 at 1:38 Comment(4)
can you get full dns record for your domain? it seems you haven't got www set as CNAME to your domainBelfry
I just added the www CNAME record above. Any ideas how to solve this?Palgrave
Do you have Namecheap SSL?Headship
I don't have SSL. I updated my question to remove the SSL part, since its the same behavior without https. Any ideas how to solve this?Palgrave
T
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
Thereunder answered 7/5, 2020 at 14:57 Comment(0)
D
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 [email protected].

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

Dexamethasone answered 3/5, 2020 at 23:2 Comment(0)
T
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
Thereunder answered 7/5, 2020 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.