Nginx Remove WWW And Respond To Both

http-status-code-301, nginx

Solution

On the first question - simply add both domains:

server_name mydomain.io www.mydomain.io;

For the second, you'll need this simple redirect:

server {
      listen 80;

      server_name www.mydomain.io mydomain.io;

      if ($host = 'www.mydomain.io' ) {
         rewrite  ^/(.*)$  http://mydomain.io/$1  permanent;
      }

Problem

I have the following nginx configuration fragment: ``` server { listen 80; server_name mydomain.io; root /srv/www/domains/mydomain.io; index index.html index.php; access_log /var/log/nginx/domains/mydomain.io/access.log; error_log /var/log/nginx/domains/mydomain.io/error.log; location ~\.php { try_files $uri =404; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_pass 127.0.0.1:9000; include /etc/nginx/fastcgi_params; } } ``` First, how can I make the server block respond to `both` http://www.mydomain.io and also http://mydomain.io. Second, I want to force if they come from http://www.mydomain.io to redirect to http://mydomain.io. Thanks.

Original source