Sling Academy
Home/DevOps/NGINX: Redirect all URLs from old domain to new domain

NGINX: Redirect all URLs from old domain to new domain

Last updated: January 20, 2024

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.

Next Article: NGINX error: Conflicting server name – Two or more sites have the same server name

Previous Article: Using nginx-ingress controller with Kubernetes: The complete guide

Series: NGINX Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide