How to redirect only the root path in nginx?

by mallory_cormier , in category: Third Party Scripts , a month ago

How to redirect only the root path in nginx?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elisha_langworth , a month ago

@mallory_cormier 

To redirect only the root path in Nginx, you can use the following configuration:

  1. Open the Nginx configuration file using a text editor. The configuration file is typically located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default.
  2. Add the following location block within the server block:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
server {
    listen 80;

    location = / {
        return 301 /new-path;
    }

    location /new-path {
        # Your configuration for the new path
    }

    # Add other location blocks for other paths if needed
}


  1. Save the configuration file and restart Nginx using the command sudo systemctl restart nginx.


This configuration will redirect requests to the root path "/" to "/new-path". You can replace "/new-path" with the desired path you want to redirect to. Make sure to add the configuration for the new path as needed in the location block specified for it.