2

I've been working on a staging site, installed to a subdomain with installatron. It appears that a good portion of this staging site has now been indexed and shows up when you search for our business. I've set up a error page with a link back to our public website, but have failed to make a 301 redirect work, with either a plugin or a cpanel redirect.

After more reading it looks like the correct answer is to put this code in the functions.php file of my staging sites child theme - but I would like someone to confirm that this will do what I'm hoping?

add_action('wp_head', 'no_robots_wp_head');
function no_robots_wp_head(){
    ?>
    <meta name="robots" content="noindex, nofollow" />
    <?php
}

In theory I want this to get google to de-index all pages of subdomain.website.ca while keeping website.ca indexed as is.

2 Answers 2

1

Use a Plugin

First of all, your CODE looks fine. However, it's better if you put that code in a small custom Plugin. That way, you can easily keep the plugin active on the development and staging sites and deactivate it on the main site.

Changing the themes back and forth in the staging and production is not a good idea. It may accidentally de-index your main site.

Also, we should have debugging enabled on development and staging sites. Often it's done by having different wp-config.php files (excluded from Git) on development, staging and production sites.

You may even take advantage of that and set the following true on dev and staging, and false on production:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );

Then never set these to true on production, at least not WP_DEBUG_DISPLAY. This way, you can just use the plugin and forget it's even there. No further change is needed from staging to production unless you change WP_DEBUG_DISPLAY on production (which you shouldn't do anyway), neither on the files, nor on the database or admin settings.

Here's how the plugin code would look like:

<?php
/*
    Plugin Name: Custom No Index
    Description: A plugin to add a noindex, nofollow meta tag to the head of every page.
    Author: Fayaz Ahmed
    Version: 1.0
*/

add_action( 'wp_head', 'no_index_on_debug' );
function no_index_on_debug() {
    if( WP_DEBUG && WP_DEBUG_DISPLAY ) {
        ?>
        <meta name="robots" content="noindex, nofollow" />
        <?php
    }
}

Use Google Search Console

Secondly, you may also use Google Search Console to expedite the process of de-indexing your staging site's pages. Here are the steps you can take:

  1. Go to https://search.google.com/search-console/ and sign in with your Google account.

  2. Select the property (website) that represents your staging site from the list of properties.

  3. From the left menu go to: IndexingRemovals, and in the Temporary Removals Tab, use the NEW REQUEST button to submit URLs for removal.

It may take some time for Google to process the request to remove the URLs from search results. You can also use the "URL inspection" tool in Search Console to check the status of your request and see if the URL has been removed from the search results.

2
  • 1
    Thank you. I've gone and moved that out of my child theme, and have successfully fixed our serp.
    – lforsey
    Commented Dec 19, 2022 at 19:15
  • @lforsey You are most welcome.
    – Fayaz
    Commented Dec 20, 2022 at 1:53
0

Why not just use the Setting under Settings>Reading Settings, tick the box that says "Discourage search engines from indexing this site".

That does the same thing as the code above, no need for any plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.