Nginx redirect image to page

Viewed 907

On a Wordpress site running on Nginx, I am trying to redirect a single image to a page.

/image/path/image.jpg => /page/to/redirect/to

A couple of things I have tried within nginx server block (after each config change, restarted the server):

rewrite /image/path/image.jpg /page/to/redirect/to redirect;
rewrite ^/image/path/image.jpg$ /page/to/redirect/to redirect;

location /image/path/image.jpg {
  rewrite /image/path/image.jpg /page/to/redirect/to redirect;
}

location /image/path/image.jpg {
  return 301 https://example.com/page/to/redirect/to;
}

I was wondering if anyone else had come across the same issue, and could point me in the direct to resolve this.

1 Answers

You need to inspect your nginx configuration, starting from /etc/nginx/nginx.conf, and make sure you are adding your code at the right location.

You need to put your code in http > server, after the "server_name", "listen", "root", however before any other "location":

http {
  server {
    server_name...
    listen...
    root...

    location /image/path/image.jpg {
      rewrite /image/path/image.jpg /page/to/redirect/to permanent;
    }

    ...
    [other location goes here]
  }
}
Related