2

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.

Nick
  • 2,593
  • 3
  • 30
  • 59
Kodie
  • 103
  • 3
  • 9
  • Any of your examples seem feasible. If they are not working, it is probably related to where you are placing the statements within the Nginx configuration or an interaction with other parts of the Nginx configuration. – Richard Smith May 13 '20 at 06:25
  • @kodie could you provide some more context? It seems it might be an ordering issue in the configuration. – Nick Jun 08 '20 at 04:50

1 Answers1

0

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]
  }
}