Dynamicly add noindex/nofollow to specific CMS pages

This is how I’ve done it.

Create a ‘Switch’ custom field called ‘Hidden’ for the collection

And finally, insert this code in the section of the template of that collection

<script>
  var meta_robots_tag = document.createElement('meta');
  meta_robots_tag.name = "robots";
  meta_robots_tag.content = Hidden ? "noindex, nofollow" : "index";
  document.getElementsByTagName('head')[0].appendChild(meta_robots_tag);
</script>

image

2 Likes

Did you have any issue using this… I want to implement this in a site which has more than 2000 blog pages.

Hi @xoelio

I ‘think’ this might be a solution to a question I’ve literally just posted at Advice? Best strategy for a 'particular' CMS Collection page to only show to Search Engines based on a TAG

So to be clear, I add another 'custom field of type ‘Switch’ to my CMS Collection, name it ‘Hidden’, and then paste the code you’ve provided into the ‘custom code’ ‘inside tag’ of the that CMS Collection’s Page Settings (see image to be sure)? And this will then allow me to determine (by toggling the Switch) which individual CMS Collection page I do and don’t want to be indexed by Search Engines?

Thank you

Stephen

Bumping @steessex’s idea here. I found this helpful article from Google that says you can use JavaScript to add a noindex, nofollow meta tag and it will work (at least that is my understanding): Understand JavaScript SEO Basics | Google Search Central  |  Documentation  |  Google Developers

I just had a situation where I combined three categories of posts: blog posts, news coverage, and press releases. This was helpful so I could put them all together in a grid (without 3rd party tools) and so I could use the same page template for them all.

Turned out our client always wanted the press releases and news coverage ones to link externally and to not be indexed.

So, I used a combo of Stephen’s idea + the ideas from that Google article and added a noindex, nofollow meta tag and also redirected to the homepage if the category was not blog posts. Here’s my code:

<script>
// excluding press releases and news coverage from being indexed for SEO
	let category = '{{THIS IS THE CATEGORY SLUG FROM WEBFLOW CMS}}';
	if (category != 'blog-post') {
  	var metaRobots = document.createElement('meta');
    metaRobots.setAttribute('name', 'robots');
    document.head.appendChild(metaRobots);
		metaRobots.setAttribute('content', 'noindex, nofollow'); // tell robots no
    window.location.href = '/'; // redirect to home page
	}
</script>

Would this cause the meta tag to be created above the script that renders it? Would that cause an issue for the google crawler to read it?

Here is the solution, it was tested on a live webiste. You just need to replace: /your-collection-page-url/ with your actual URL, and thre you have it, you can set any single collection page to noindex. Also, this code should be added in head of Collection Page Template:

<script>
  // Check if the current page URL matches the single collection page URL
  if (window.location.pathname === '/your-collection-page-url/') {
    // Find the existing meta tag with the name attribute set to 'robots'
    var metaTag = document.querySelector('meta[name="robots"]');

    // Check if the meta tag exists
    if (metaTag) {
      // Update the content attribute to 'noindex'
      metaTag.setAttribute('content', 'noindex');
    } else {
      // Create a new meta tag with the name and content attributes set to 'robots' and 'noindex' respectively
      metaTag = document.createElement('meta');
      metaTag.setAttribute('name', 'robots');
      metaTag.setAttribute('content', 'noindex');
      document.head.appendChild(metaTag);
    }
  }
</script>