How to redirect from one domain to another in Nginx and Debian 11

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. We know that web servers are an important and indispensable part of the community. That’s why lately, we have dedicated some tutorials on Nginx. Today we will do another one. Today, you will learn how to redirect from one domain to another in Nginx and Debian 11.

Before we get started …..

Before we continue, we have to make sure that we meet the requirements to complete the post without problems

  • You need to have Nginx installed and configured. So make sure you have it using the official repositories of your Linux distribution.
  • The version of Nginx must be higher than 1.9.5 You should have no issue to have it because most distributions include recent versions.
  • Have basic knowledge on the use of the terminal. A user with sudo access or root access.

Let’s go for it.

Redirecting from one domain to another in Nginx and Debian 11

Normally, you will have several configuration files on your server. That’s why you have to edit the configuration file for the domain you are redirecting to.

For this post, I will redirect from / to the new domain for example fbi.unixcop.com.

To achieve this, you just have to open the configuration file of the website

And inside the file you have to add something similar to this inside the server section

location / {
        rewrite ^/(.*)$ https://fbi.unixcop.com/ redirect;
    }
redirect from one domain to another in Nginx and Debian 11
redirect from one domain to another in Nginx and Debian 11

Save the changes and close the editor.

The section we have modified is easy to explain. location / indicates that we will work from the root of the site. rewrite indicates rewriting an address. ^/(.*) is for all pages within the entire site. $ indicates the end of the string. Next, you define where to redirect to. Finally, redirect states that there will be a redirect.

Check the Nginx syntax

sudo nginx -t

And apply the changes by restarting the service

sudo systemctl restart nginx

This way we will be able to achieve the goal without too many problems.

Conclusion

Redirecting is a task that many sysadmins have to do when managing websites. Doing it with Nginx is basic and can save us a lot of trouble.

Nginx documentation

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook