How To Fix Nginx 301 Redirect Without Trailing Slash?

Published September 17, 2024

Problem: Nginx 301 Redirect Without Trailing Slash

Nginx servers often have issues with 301 redirects when handling URLs without trailing slashes. This can cause unexpected behavior, affect website navigation, and impact search engine optimization. Fixing this problem helps maintain a good user experience and proper URL structure.

Solving the Nginx 301 Redirect Problem

Using the absolute_redirect Directive

The absolute_redirect directive in Nginx controls how redirects are handled. When set to "off," it stops Nginx from generating absolute redirects. This means the server will use relative URLs for redirects, keeping the original port number and protocol.

To use the absolute_redirect directive in your Nginx configuration:

  1. Open your Nginx configuration file (/etc/nginx/nginx.conf or /etc/nginx/sites-available/default).
  2. Add this line within the server block:
absolute_redirect off;

Tip: Debugging Redirects

Use browser developer tools or a command-line tool like curl to check the redirect responses. This helps verify if the absolute_redirect directive is working as expected.

Configuring the Server Block

To change your Nginx configuration file and fix the 301 redirect issue:

  1. Open your Nginx configuration file with a text editor using root privileges.
  2. Find the server block for your website.
  3. Add the absolute_redirect directive as shown above.
  4. Save the changes and close the text editor.
  5. Test the configuration for syntax errors:
nginx -t
  1. If there are no errors, reload Nginx to apply the changes:
nginx -s reload

Here's an example of a configured server block:

server {
    listen 80;
    server_name example.com;
    absolute_redirect off;

    location / {
        root /var/www/html;
        index index.html index.htm;
    }

    # Other location blocks and configurations...
}

This configuration will stop Nginx from removing the port number during redirects and maintain the expected behavior when accessing URLs without trailing slashes.

Testing the Solution

Using cURL to Verify Redirects

You can use cURL to check if the Nginx configuration changes have fixed the redirect issue. This tool helps you test HTTP requests and examine server responses.

To use cURL for testing redirects:

  1. Open a terminal or command prompt.
  2. Run this command:
curl -I http://localhost:8080/test

This command sends a HEAD request to the URL and displays the response headers.

Interpreting the cURL output:

  • Look for the "Location" header in the response.
  • If the configuration is correct, you should see a relative URL like "/test/" instead of an absolute URL.
  • Check that the status code is 301 (Moved Permanently).

Example of a correct cURL output:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0
Date: Mon, 01 May 2023 12:00:00 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: /test/

Testing Multiple URLs

To test multiple URLs quickly, create a shell script that loops through a list of URLs and runs cURL for each one. This can save time when checking various endpoints on your site.

#!/bin/bash
urls=("test" "about" "contact" "products")
for url in "${urls[@]}"
do
  echo "Testing http://localhost:8080/$url"
  curl -I "http://localhost:8080/$url"
  echo "-------------------------"
done

Browser Testing

After verifying the redirect behavior with cURL, test the fix in different web browsers to make sure it works for regular users.

Steps for browser testing:

  1. Open various web browsers (e.g., Chrome, Firefox, Safari, Edge).
  2. In each browser, enter the URL without a trailing slash (e.g., http://localhost:8080/test).
  3. Check if the browser keeps the port number in the address bar after the redirect.
  4. Verify that the page loads correctly with the trailing slash added.

If the fix works in all tested browsers, you have solved the Nginx 301 redirect issue. Users can now access your website pages with or without trailing slashes, and the port number will be kept in the URL when needed.