Skip to content

Noindex in .htaccess

I don't want search engine robots to index my pages, but I can't get to the site's code to add a robots meta tag - what now? The answer is the right rules in the .htaccess file.

When it comes to blocking indexing, besides <meta> tags there is the local Apache configuration file, where we have already added 301 redirects or switched on gzip compression more than once - .htaccess.

Noindex for PDF files

For files with a given extension (type), such as PDF, there is a simple and effective solution suggested by Google. This adds "noindex, nofollow" to all PDF files or image files:

# A rule for one file type (PDF):
<Files ~ "\.pdf$">
  Header Set X-Robots-Tag "noindex, nofollow"
</Files>

# A rule for several types at once:
<Files ~ "\.(png|jpe?g|gif)$">
  Header Set X-Robots-Tag "noindex, nofollow"
</Files>

Hold on! This is not the good old "robots" meta tag - there is no change in the page code at all! Not to mention that no human ever looks inside a PDF file for meta tags...

Right, because X-Robots-Tag is an HTTP response header, which you can see for example in the Network tab of the developer tools:

Browser developer tools, Network tab: HTTP response headers with X-Robots-Tag: noindex, nofollow
HTTP headers with X-Robots-Tag

If you want to block indexing of the files in a given folder from the root folder of the domain (normally you would put such a file in that folder), use the rules you will find in the examples.

Noindex for a URL

You can add the X-Robots-Tag header to any URL as well. It takes a simple set of rules that work in three steps:

  • Check the address,
  • Set an environment variable,
  • Set the HTTP header

So for addresses that contain the word "test", the header mentioned above, "noindex, nofollow", will be set:

RewriteEngine On

RewriteRule test - [E=NOINDEX_HEADER:1]
Header Set X-Robots-Tag "noindex, nofollow" ENV=REDIRECT_NOINDEX_HEADER

Try changing "REDIRECT_NOINDEX_HEADER" to "NOINDEX_HEADER" if these rules do not work (explanation below)

Two lines are enough to add the header we want. You may also notice that the name of our environment variable changed from NOINDEX_HEADER to REDIRECT_NOINDEX_HEADER. That is because this code runs in a WordPress setup, where right after it "everything" is rewritten to index.php. This is how Apache marks variables that still exist after an internal redirect - it adds the "REDIRECT_" prefix. So if you use the rule for folders, files or in another setup, remember to use the variable name without the prefix.

You will find more examples a few paragraphs below.

Meta tag versus HTTP header

Google treats the X-Robots-Tag header the same way as the robots meta tag in the <head> section of the page's source code.

Watch out! If the robots meta tag and the X-Robots-Tag header are set at the same time to different values, e.g.

<meta name="robots" content="index">
Header Set X-Robots-Tag "noindex"

the most restrictive value wins, here "noindex", so Google will not index the resource. You can also expect it to be reported as an error in Google Search Console. Other search engines, such as Bing, may of course behave differently.

Do not block these addresses in robots.txt. If a robot cannot fetch an address because robots.txt forbids it, it will not see the "noindex" header either - and such an address can then hang around in the search results without a description. Noindex first, and robots.txt only once the addresses have left the index.

Examples

Below are a few ready-made examples (use cases) for common situations:

A filter in a shop

Say your shop has a link like this:

example.com/pl/c/courses/12/1/default/1/f_producer_3/

The part in bold carries the value of the filter the visitor chose, here by manufacturer. We do not want such a link indexed. Since we want this for all manufacturers, let's assume they are marked by "f_producer_" (followed by the manufacturer's ID in the system, here "3"). We can do it by adding this code to .htaccess:

RewriteEngine On

RewriteRule f_producer_ - [E=NOINDEX_HEADER:1]
Header Set X-Robots-Tag "noindex, nofollow" ENV=REDIRECT_NOINDEX_HEADER

OK, that works for a simple URL without parameters (a query). To check an address with parameters, like this one:

example.com/pl/c/courses/12?f_producer=3

we use the code below (note the variable %{QUERY_STRING}, used to look at the parameters):

# %{QUERY_STRING} lets us check the fragment
# "f_producer=3"
# that is, the parameters

RewriteEngine On
RewriteCond %{QUERY_STRING} f_producer=
RewriteRule .* - [E=NOINDEX_HEADER:1]
Header Set X-Robots-Tag "noindex, nofollow" ENV=REDIRECT_NOINDEX_HEADER

Make sure the fragment you block is not also part of, say, the URL of the manufacturer's page!

Files in a folder

Say you have a folder where you put all your PDF files, such as offers or product sheets, which gives you links like these:

  • example.com/upload/products/book-one.pdf
  • example.com/upload/products/book-two.pdf
  • ...

You can put these rules in .htaccess to apply noindex to such files:

RewriteEngine On

RewriteRule ^\/upload\/products\/.*\.pdf$ - [E=NOINDEX_HEADER:1]
Header Set X-Robots-Tag "noindex, nofollow" ENV=REDIRECT_NOINDEX_HEADER

This example uses a proper regular expression, made of the start-of-string sign "^", the escape character "\" (backslash), the expression ".*" that accepts any number of any characters, and the end-of-string sign "$". REGEX - highly recommended, great fun 😉

Troublesome URLs

This example is a bit out of context, but anyone who has tried to tame a home-grown CMS, or even a popular one with poor handling of internal addresses, will recognise it.

Say your CMS puts needless links to its content modules into the page - just an oversight by its developers or architects. Something like this:

  • example.com/component/download/file.pdf
  • example.com/index.php?mod=component&download=file.pdf

That is needless links in the code, and twice over - as a "friendly URL" and as a parameter-based address. The rule below cuts out both cases:

# %{THE_REQUEST} lets us check the request sent to the server
# "GET /index.php?mod=component&download=file.pdf HTTP/1.1"

RewriteEngine On
RewriteCond %{THE_REQUEST} component
# Swap the RewriteCond above for the one below for a smaller margin of error
# RewriteCond %{THE_REQUEST} (mod=component|\/component\/)
RewriteRule .* - [E=NOINDEX_HEADER:1]
Header Set X-Robots-Tag "noindex, nofollow" ENV=NOINDEX_HEADER
Header Set X-Robots-Tag "noindex, nofollow" ENV=REDIRECT_NOINDEX_HEADER

Looks rather complicated, doesn't it? The double "Header Set..." line is there because the standard setup of a CMS or shop rewrites the friendly address (the variable gets the "REDIRECT_" prefix), while a request for an actual file is not rewritten (no prefix).

Mistakes

When adding rules to .htaccess you may run into a few errors. Some show up immediately, others only in their effects - be careful.

Always check the "changed" URLs and the critical addresses of the system when you change .htaccess, for example when adding redirects. Check the contact forms, the cart page and the checkout path.

Error 500

You will hit this error right after uploading your changes to the server. It mostly means a syntax error or a missing Apache module:

  • a typo in the rules,
  • a syntax error, e.g. from pasting part of a URL,
  • a stray space, e.g. pasted along with the rules,
  • the mod_headers module is not active in Apache (install and enable it, or ask your webmaster or provider to do it). On Debian/Ubuntu, for example, two commands in the terminal do it - the first enables mod_headers, the second restarts Apache so it loads the new configuration:
sudo a2enmod headers
sudo systemctl restart apache2

The rules do not work

This happens a lot, so it is worth checking whether the rules look at variables that can contain what you are looking for at all. Say you have the address "example.com/category?string=test" and try to catch it with this rule:

RewriteCond %{REQUEST_URI} test

# the rest of the code...

No wonder it does not work: %{REQUEST_URI} does not include the part after the "?", the parameters. You should use %{QUERY_STRING}. Why? See the most used mod_rewrite variables in Apache, which come in handy when checking addresses. If you want to see what these variables contain, have a look at the article on debugging .htaccess.

The rules hit other addresses or files

Check whether the expression you are looking for is too general. Using "product", for example, will deindex the filters, but also the products. Try changing the pattern, e.g.:

  • by adding the characters that come with it (an underscore or a hyphen, for example),
  • by adding start or end-of-string signs to the regular expression (^ / $),
  • by writing a more elaborate regular expression (REGEX),
  • by using another variable (REQUEST_URI, QUERY_STRING, THE_REQUEST).

The rules still do not work, or always work

It can happen that the settings you added still do not work (even though debugging shows the right URL), or the opposite - they work for any address, even though you added a <Files> or <FilesMatch> directive or the right RewriteCond to limit the rules to the narrow set of URLs you care about.

First, you do not know the full server configuration (if all you have is .htaccess over FTP). Then it is worth asking the "server people" why your rules do not behave as expected. In other words, your last resort, and usually a very effective one, is the admins at your company or your host's support team.

Second, an .htaccess file that seems to work normally does not have to be used by Apache at all. Many server products, such as LiteSpeed or IdeaWebServer, whatever they run "under the hood", are compatible with the idea of passing settings in an .htaccess file. They read the file and translate its settings into their own format, better or worse. That is great, because you can use the rules you know or install scripts you know (which would not fly with nginx, for example). With popular (and simple) rules you may not notice any difference. Sometimes, though, these programs skip some directives or parse them incompletely or wrongly - in other words, bugs. Such bugs can, for example, skip a directive's declaration but still read its contents (so the rules start working for any URL). IdeaWebServer, for one, does not support <FilesMatch> and needs a <Files> directive instead - so it is best to find out what you are actually running on and apply the fixes from that server's documentation, to avoid rules that "still do not work".

Advertisement