Nginx — Redirect Only Root URL Path

We recently launched an internal website. This website doesn’t have a landing page and provides the feature on a URL path. We wanted to redirect all requests navigating the root path / to the feature page using nginx. Here’s how we solved it.

nginx Series Overview

Redirect the Root URL Path

This nginx rule redirects only the root URL to the defined domain:

location = / {  
    return 301;

The = in location = / specifies an exact match of the URL. This rule won’t match if anything precedes or follows the forward slash. Make sure to define another rule that handles URLs other than the root path.

Explore the Library

Find interesting tutorials and solutions for your problems.