I want to add X-Robots-Tag noindex, nofollow
to the HTTP response of all the .PDF files of a site to avoid that these documents be referenced by the Google search engine.
This is for Tomcat 8 server on Heroku with Spring boot 2.1. From the past, I've tried on Apache Server and the noindex
and nofollow
worked well.
<Files ~ "\.pdf$">
Header set X-Robots-Tag "noindex, nofollow"
</Files>
You could create a servlet filter that does this for you like.
@WebFilter(urlPatterns = {"*.pdf"})
public class PdfFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpServletResponse = (HttpServletResponse)response;
httpServletResponse.addHeader("X-Robots-Tag", ""noindex, nofollow");
chain.doFilter(request, response);
}
}