How to redirect a directory in nginx server?

by wilmer.lemke , in category: Third Party Scripts , a month ago

How to redirect a directory in nginx server?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , a month ago

@wilmer.lemke 

To redirect a directory in nginx server, you can use the "return" directive in the server block of your nginx configuration file. Here is an example of how you can redirect a directory:

  1. Open your nginx configuration file. This file is usually located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default.
  2. Add the following lines inside the server block:
1
2
3
location /old-directory {
    return 301 /new-directory;
}


In this example, any requests to "/old-directory" will be redirected to "/new-directory" with a 301 status code (permanent redirect).

  1. Save the configuration file and reload nginx to apply the changes:
1
sudo nginx -s reload


Now, any requests to the old directory will be automatically redirected to the new directory.