How to Redirect from One Domain to Another in Nginx and Debian 11
If you’re moving a site from example.tld to newdomain.tld, this guide shows you how to make the switch cleanly on Debian 11 with nginx.
Redirect one domain to another using server blocks
1. Create a fresh config file for your old domain
sudo nano /etc/nginx/sites-available/example.tld
This keeps things tidy instead of tacking the redirect into an unrelated block.
2. Add a minimal server stanza
server {
listen 80;
server_name example.tld www.example.tld;
return 301 $scheme://newdomain.tld$request_uri;
}
The return directive is faster than a rewrite and tells nginx to hand the request over immediately. Using $request_uri preserves the path and query string so /blog/2023 still lands at newdomain.tld/blog/2023.
3. Enable the site
sudo ln -s /etc/nginx/sites-available/example.tld /etc/nginx/sites-enabled/
4. Check the syntax
sudo nginx -t
If it reports any errors, double‑check that you didn’t miss a brace or forget to escape a $.
5. Reload nginx
sudo systemctl reload nginx
The old domain will now bounce straight to newdomain.tld with a 301 status code.
6. Verify the redirect
Open http://example.tld in your browser and confirm you’re taken to https://newdomain.tld (assuming you’ll set up HTTPS on the new domain). You can also run curl -I http://example.tld to see the HTTP/1.1 301 Moved Permanently header.
Why this approach beats other tricks
Some people sprinkle rewrite rules inside their main site block, which clutters the file and adds overhead. The return directive is a one‑liner that does exactly what you want with no extra processing. I’ve seen sites stuck with old redirects for weeks because they kept using complicated regexes—no one noticed until Google flagged duplicate content.
What if you need HTTPS on the old domain?
If you still have an SSL cert for example.tld, add a second server block listening on 443 and point it to the same return line. That way users who type https://example.tld are redirected instantly without any handshake hiccups.
When not to bother with nginx
If your host only gives you cPanel or Plesk, configuring redirects in an .htaccess file might be easier. But on a Debian 11 server where you own the box, nginx is lightweight and perfect for this job—no need to over‑engineer it.
That’s it—your old domain now hands over the baton gracefully.