Cookie Consent by Free Privacy Policy Generator

Tutorial: Adding NoIndex Tags to Multiple URLs (PHP & SQL)

If you're grappling with an older PHP Content Management System (CMS) or eCommerce Platform that lacks modern features and flexibility, specifically the straightforward application of a noindex meta tag across multiple URLs, then I have a quick and easy to deploy solution for you.

This methodology can handle the implementation of noindex tags across multiple URLs in a more dynamic and efficient manner to help you with your SEO strategies.


Here's a step-by-step guide on how to set it up:

Guide Contents
Step 1: Create SQL Database Table
Step 2: Add URLs to the SQL Table
Step 3: Implement the PHP Script
Step 4: Monitor Your Website


Step 1: Create SQL Database Table
db1.jpg


Before we begin, make sure you have a SQL database ready for use. If you haven't already created one, you'll need to do so through your web host's control panel or by using a tool like phpMyAdmin.

Next, we need to create a SQL table that will hold the URLs we want to apply the noindex tag to. You can create the table using the following SQL command:

PHP:
CREATE TABLE noindex_urls (
    id INT AUTO_INCREMENT PRIMARY KEY,
    url VARCHAR(255) NOT NULL
);


This will create a table named noindex_urls, with an id column that auto-increments and a URL column where we will store the URLs.


Step 2: Add URLs to the SQL Table
db2.jpg


Once your table is created, you can add the URLs using the following SQL command:
PHP:
INSERT INTO noindex_urls (url) VALUES ('http://example.com/noindex1'), ('http://example.com/noindex2'), ('http://example.com/noindex3');

Remember to replace 'http://example.com/noindex1', 'http://example.com/noindex2', and 'http://example.com/noindex3' with your actual URLs.



Step 3: Implement the PHP Script

Next, you need to include the PHP script in your CMS. You need to replace the database connection parameters ($dbHost, $dbUsername, $dbPassword, $dbName) in the script with your actual database details.

The script fetches the URLs from your database, converts them to lowercase, and adds them to an array. It then checks if the current URL is in this array, and if it is, it outputs the noindex meta tag.

The script needs to be added to within the <HEAD> </HEAD> tags.
PHP:
<?php
// Database connection parameters
$dbHost     = 'localhost';
$dbUsername = 'root';
$dbPassword = 'password';
$dbName     = 'database_name';

// Create connection
$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Fetch URLs from the database that should be noindexed
$sql = "SELECT url FROM noindex_urls";
$result = $conn->query($sql);

$noindexURLs = array();

if ($result->num_rows > 0) {
    // Output each row into the array
    while($row = $result->fetch_assoc()) {
        $noindexURLs[] = strtolower($row["url"]);
    }
} else {
    echo "0 results";
}
$conn->close();

// Get the current URL
$currentURL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$currentURL = strtolower($currentURL);

// Check the condition - if the URL is in the noindexURLs array
if (in_array($currentURL, $noindexURLs)) {
    // If the condition is met, output the 'noindex' meta tag
    echo '<meta name="robots" content="noindex">';
}
?>


Step 4: Monitor Your Website

Now that everything is set up, keep an eye on your website and its performance. Remember that the noindex tag instructs search engines not to index a page, which can affect the visibility of your pages in search results. Use this tag wisely! I would recommend that you crawl your website using a tool such as Screaming Frog to check which URLs that you deployed the noindex meta tag on.

That's it! This simple solution should help to deploy the noindex meta tag across multiple URLs on your PHP CMS. Let me know if you have any questions or run into any issues by commenting below.

If the above is a little too complex and you don't feel comfortable implementing it, I wrote another tutorial that you can use Google Tag Manager instead. Visit: https://chrisleverseo.com/t/noindex-metarobots-urls-with-tag-manager.48/
 
Last edited:
Back
Top