How to redirect a few urls in nginx?

by jose_gulgowski , in category: SEO , 6 months ago

How to redirect a few urls in nginx?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by susan , 6 months ago

@jose_gulgowski 

To redirect a few URLs in Nginx, you can use the return directive. Here is an example configuration snippet to redirect multiple URLs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
server {
    listen 80;
    server_name example.com;

    location /old-url1 {
        return 301 http://example.com/new-url1;
    }

    location /old-url2 {
        return 301 http://example.com/new-url2;
    }

    location /old-url3 {
        return 301 http://example.com/new-url3;
    }

    location / {
        # Handle other requests
        ...
    }
}


In the above configuration, we define a server block with the server_name directive set to your domain name. Then, we define multiple location blocks for each URL you want to redirect.


Within each location block, we use the return directive with a 301 status code to perform the redirect. Replace the example URLs (new-url1, new-url2, new-url3) with the actual target URLs you want to redirect to.


Make sure to reload or restart Nginx after making these changes for the configuration to take effect.