NGINX: Redirect all URLs from old domain to new domain

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

Migrating to a new domain can be a fundamental step for a brand’s evolution. However, ensuring that visitors trying to access the old domain are seamlessly redirected to the new one can be a technical challenge. In this tutorial, we will go through the process of configuring NGINX to redirect all URLs from an old domain to a new domain, preserving the URI and query strings in the process.

Prerequisites

  • A working NGINX installation
  • Access to the NGINX configuration files
  • Both the old and new domain names properly set up and pointing to your server

Understanding NGINX Configuration Files

Before diving into the redirection rules, it’s important to understand where these configurations will be placed. NGINX configuration files are typically located in /etc/nginx/nginx.conf or in specific files under the /etc/nginx/sites-available/ directory, with symbolic links in /etc/nginx/sites-enabled/.

Create a Backup

Always start by creating a backup of your current NGINX configuration:

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup

Redirecting with NGINX

Creating Permanent Redirects (301)

A 301 redirect signals to search engines that a page has moved permanently, which is essential for maintaining SEO rankings when you change domain names. Here’s how you can create a server block in your configuration to handle the redirection:

server {
    server_name olddomain.com www.olddomain.com;

    location / {
        return 301 $scheme://newdomain.com$request_uri;
    }
}

This block listens for requests to olddomain.com or www.olddomain.com and returns a 301 redirect response with the new domain but preserves the rest of the URL.

Reloading NGINX Configuration

After saving your changes, you must reload your NGINX configuration for the changes to take effect:

sudo nginx -t
sudo systemctl reload nginx

The nginx -t command tests your configuration for syntax errors before you apply the changes with a reload.

Redirection Variations

Redirecting with HTTPS

With the growing importance of secure connections, you may want to ensure that the redirection happens over HTTPS. Adjust your server block to accommodate this:

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

With this setup, all incoming requests on HTTP are redirected to HTTPS on the new domain.

Preserving Protocol and Subdomains

If you wish to maintain the protocol used (HTTP or HTTPS) or to handle subdomains dynamically, you will need to work with NGINX variables:

server {
    server_name ~^(wwwackslash.)?(?.*).olddomain.com$;

    return 301 $scheme://$sub.newdomain.com$request_uri;
}

Here, we are using a regular expression to capture any subdomain and append it to the new domain URL.

Troubleshooting Common Errors

When configuring redirects, you might encounter issues. Here’s how to troubleshoot some common problems:

  • Configuration Syntax: Always check your configuration files with nginx -t before reloading.
  • Clearing Cache: Browsers cache 301 redirects, so you might need to use incognito mode or clear your browser cache if redirection isn’t working immediately.

Conclusion

Setting up domain redirection with NGINX requires only a few lines of configuration but it is important to understand the consequences of each rule. Properly setting up a redirect will ensure that your website visitors have a smooth transition to your new domain, and your website’s SEO rankings preserve their integrity.

As always, test your NGINX configurations thoroughly and keep a backup handy before making any changes to your live server. With this guide, you should be able to confidently navigate domain redirections with NGINX.