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;
}
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.