How To Redirect in Nginx all Domain Name Versions to https://www

| Servers & Config | 22 seen

As you might already know search engines (Google) sees www.domain.com and domain.com as two separate domain names. If you are not using 301 redirects (www to non-www or vice verse)you might get penalized for duplicate content. 

In Ngnix, there is a simple solution how to make such redirect happen, see: Ngnix Redirect To WWW

Now, things get a bit complicated when https versions are involved. See: Linode: How To Secure Nginx with Let's Encrypt on Ubuntu 12.04

Once your website has https enabled, technically speaking you might be dealing with for different domain names:

  1. domain.com
  2. www.domain.com
  3. https://domain.com
  4. https://www.domain.com

What we are looking is to redirect all versions to one, I prefer the version with https://www, here is how to achieve that with Nginx server blocks.

Assuming you have already configured server blocks pointing your domain name to https://www open your server config:

sudo nano /opt/etc/nginx/sites-available/reinisfischer.com

And at the end of the code add:

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

Now, restart Nginx

sudo /etc/init.d/nginx restart

Hope it helps!

 

Latest articles

Berlin Zoo and Aquarium: One of the Best Things To Do in Berlin With Kids

At the start of April 2026, during our roughly five-day trip to Berlin, we finally visited the famous Berlin Zoo for the first time - or at least the first time I can actually remember. There is a chance my mom might have taken me here sometime in the early 1990s, but if so, those memories are long gone. This visit felt completely new, and…

Germany |

Berlin Brandenburg Airport

In April 2026, I had my first experience with Berlin Brandenburg Airport (BER) while flying the Riga–Berlin–Riga route with airBaltic. This was my first time using Berlin’s main international airport, and overall, it left a solid impression—modern, functional, and relatively easy to navigate. About a decade ago, I had the chance to travel through…

Germany |