Skip to content

Instantly share code, notes, and snippets.

@huytrq
Created March 4, 2021 02:44
Show Gist options
  • Select an option

  • Save huytrq/8be87fa075d65e54dda42fd1d4d946c8 to your computer and use it in GitHub Desktop.

Select an option

Save huytrq/8be87fa075d65e54dda42fd1d4d946c8 to your computer and use it in GitHub Desktop.
Prevent a specific PDF file from being indexed by search engines in Wordpress
# The <Files> directive only applies to filenames, not file-paths, so your <Files> directive will never match and the header will not be set.
# To set this header on a specific file (and not all .pdf files - as in the linked question/answers) in the root .htaccess file then you can set an environment variable when this file is requested and conditionally set the header based on this env var.
# For example:
SetEnvIf Request_URI "/path/to/example.pdf" NOINDEX
Header set X-Robots-Tag "noindex, nofollow" env=NOINDEX
# Alternatively, if you could place an additional .htaccess file in the directory that contains the PDF file you want to target, then you could use a <Files> directive in that .htaccess file:
<Files "example.pdf">
Header set X-Robots-Tag "noindex, nofollow"
</Files>
# You could use this same method in the root .htaccess file, but it will also add the header to all example.pdf file requests on the system - although it's probably unlikely you have more than one file with the same name anyway I would think, so this may be the better solution after all.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment