I need to add a noindex tag to multiple specific uploaded files, but they all belong to different year and month upload folders like this for example:
/var/www/public/app/uploads/2021/06/file-1.pdf/var/www/public/app/uploads/2018/11/file-2.pdf/var/www/public/app/uploads/2011/07/file-3.pdf
I did this initially, but did not work:
location ~ "^(.*)/app/uploads/(.*)$" {
location ~ ^/(file-1.pdf|file-2.pdf|file-3.pdf)$ {
add_header X-Robots-Tag "noindex";
}
}
I don't have access yet to test, but this is what I'm planning to do instead:
location ~ "/var/www/public/app/uploads/(.*)$" {
location ~ ^/(file-1.pdf|file-2.pdf|file-3.pdf)$ {
add_header X-Robots-Tag "noindex";
}
}
I wonder if there is a better approach, or if this will even work. Right now there are 27 specific files I have to do this to so I'm not sure if the file-1.pdf|file-2.pdf|file-3.pdf is my best option.
Any help is appreciated, thanks.
First of all, you are definitely use the
locationdirective incorrectly. Assuming your server root directory is/var/www/public/appand your sample request ishttp://example.com/uploads/2021/06/file-1.pdfthe normalized URI (which is subject to check forlocationorrewritedirectives) will be/uploads/2021/06/file-1.pdf. An example of location that will catch those requests and add a required header isHere
(?<file>.*)is so-called named capture group for later usage. Using two nested locations is possible too:You can also use
mapdirective (although I don't know if this could be called "a better approach"):or without regexes:
The
add_headerdirective won't add a header to response if its second parameter will be an empty string.You should also take in attention this documentation excerpt: