How to redirect a directory in nginx server?

by wilmer.lemke , in category: Third Party Scripts , 5 months ago

How to redirect a directory in nginx server?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , 5 months 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.

Related Threads:

How to redirect port 80 to different server in nginx?
How to redirect only the root path in nginx?
How to redirect to a custom url with nginx?
How to redirect url with proxypass using nginx?
How to redirect an encoded url slug with nginx?
How to redirect ip to https domain in nginx?