How To Redirect A Domain And Its Subdomains Using Nginx?

Published July 12, 2024

Problem: Redirecting Domains and Subdomains with Nginx

Redirecting a domain and its subdomains can be difficult, especially when using Nginx as the web server. This process involves setting up configurations to send all traffic from the original domain and its subdomains to the desired destination.

Solution: Configuring Nginx for Domain Redirection

Setting Up the Nginx Server Block

To redirect a domain and its subdomains using Nginx, you need to create a server block in your Nginx configuration file. This block will define the rules for handling requests to your domain.

Open your Nginx configuration file and add a new server block:

server {
    # Configuration will go here
}

Define the server_name directive to specify which domain and subdomains this block should handle. Nginx allows you to match both the main domain and all subdomains using a dot prefix:

server {
    server_name .mydomain.example;
    # More configuration will follow
}

This server_name setting will match "mydomain.example" and any subdomain of "mydomain.example".

Implementing the Redirection Rule

Now you can add the redirection rule. Nginx offers ways to implement redirects, but a common method is using the rewrite directive.

Add this line inside your server block:

server {
    server_name .mydomain.example;
    rewrite ^ http://www.adifferentdomain.example$request_uri? permanent;
}

This rewrite rule does the following:

  • The ^ symbol matches the beginning of the URI.
  • http://www.adifferentdomain.example is the target domain for the redirect.
  • $request_uri captures the original request URI and adds it to the new domain.
  • The ? at the end removes any query strings from the original request.
  • The permanent flag creates a 301 (permanent) redirect.

This configuration will redirect all requests to "mydomain.example" or any of its subdomains to "www.adifferentdomain.example", keeping the original path and removing any query strings.

By setting up your Nginx configuration this way, you create a redirection system that handles both your main domain and all its subdomains.

Tip: Testing Your Nginx Configuration

After making changes to your Nginx configuration, it's important to test it before reloading the server. You can use the following command to check for syntax errors:

nginx -t

If the test is successful, you can then reload Nginx to apply the changes:

nginx -s reload

Alternative Methods for Nginx Redirection

Using the Return Directive

For Nginx versions 0.9.1 and higher, you can use the return directive to create a simpler redirection rule. This method is clear and efficient.

To implement this, modify your server block as follows:

server {
    server_name .mydomain.example;
    return 301 http://www.adifferentdomain.example$request_uri;
}

This setup does the same as the rewrite method, but with a shorter syntax. The 301 status code shows a permanent redirect, and $request_uri keeps the original path in the new URL.

Tip: Use HTTPS for Secure Redirects

When setting up redirects, it's a good practice to use HTTPS instead of HTTP for the target URL. This improves security and helps maintain SSL/TLS encryption. Here's an example:

server {
    server_name .mydomain.example;
    return 301 https://www.adifferentdomain.example$request_uri;
}

Wildcard Subdomain Handling

To handle all subdomains, including future ones, you can use wildcard patterns in your server_name directive. This method allows for new subdomains without changing the setup.

Here's how to set it up:

server {
    server_name mydomain.example *.mydomain.example;
    return 301 http://www.adifferentdomain.example$request_uri;
}

This setup captures both the main domain and any subdomain, redirecting them all to the target domain. The wildcard (*) matches any subdomain prefix, allowing for flexibility in your redirection setup.