Today we will look at an interesting task: how to disable indexing of add-to-cart, that is, a URL with a query string, ?add-to-cart= parameters in WooCommerce, and we will solve it in two ways.

Task description

The WooCommerce plugin can generate a huge number of links like https://site.com/?add-to-cart=111, and after site.com/ there can be a category or tag name. Such links are found by search engine bots, indexed, after which you get an warning, for example, in the Google Search Console about duplicate pages, non-canonical pages, etc.

Why is this happening?

Links /?add-to-cart=111 are placed as “Add to Cart” or “Buy” buttons on archive pages, as well as in widgets:

  • product categories
  • product tags
  • widget “Related products”
  • block “You may also be interested”
  • widget “Recently viewed products”
  • any other WooCommerce widgets or blocks that display a product thumbnail and a button to add it to the Cart.

When you click on such a button, the product is added to the cart, but the same page opens. That is, there is no real page under the link with /?add-to-cart=111. Thus, the search bot indexes the page duplicate.

The solution of the problem

There are two ways to prevent /?add-to-cart links from being indexed: in the robots.txt file and in the .htaccess file. We recommend combining these methods to achieve a 100% guaranteed result.

Disable indexing in robots.txt

The robots.txt file is located in the root folder of your site and is available via a link like https://site.com/robots.txt. If there is no physical file in the folder, WordPress generates a virtual robots.txt file.

When you have such a file, then check for the following line in it, which prohibits search engines from crawling links with the add-to-cart parameter:

Disallow: *?add-to-cart*

If there is no such line, add it manually (use an FTP client).

And when there is no robots.txt file in the root folder at all, we recommend using the SpeedPlus OptiMini plugin, which automatically creates the correct robots.txt file.

Запрет индексации add-to-cart

Meanwhile, adding such a rule is not a complete guarantee that the search engine will obey. Bots are often self-willed and after that messages “Scanned despite the noindex…” appear in the Search Console. And then we use an additional method.

Disable indexing add-to-cart in .htaccess file

The .htaccess file is also located in the root folder of the site. This is the most important file on the entire site, as it is loaded first and tells the browser, among other things, how to open links. The file can be edited exclusively in direct access, for example, using an FTP client. And don’t forget to back up the file.

To prevent links with the ?add-to-cart= query string from being indexed, you should add the following code to .htaccess:

# Disable indexing of "?add-to-cart=" pages by Speedplus.com.ua
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} add-to-cart= [NC]
RewriteRule .* - [E=NOINDEXME:1]
Header set X-Robots-Tag "noindex, nofollow" env=NOINDEXME
</IfModule>

After adding this code, pages with the ?add-to-cart= parameter will be assigned the X-Robots-Tag HTTP-header, which, although not visible in the page source code, is located in the server configuration file. This setting tells search engine bots not to index the page even without loading the page content. Fantasy, right?

You can check if the HTTP header is added to the pages of your site using a free service. To be more sure that the code works correctly, enter two URLs of your website page into the query text block at once: with and without a query string.

https://site.com/
https://site.com/?add-to-cart=111
Добавление X-Robots-Tag: noindex nofollow

Successful results of parsing the second address (with parameters) should contain the line:

X-Robots-Tag: noindex, nofollow

Thus, now you have doubly disabled the indexing of duplicate pages with ?add-to-cart= in the URL.

If you have questions, ask them in the comments to this article. If you need revision of this code or custom development, please contact us directly (contacts).

Leave a comment

Your email address will not be published.