How to Expire DoFollow Links Automatically (SEO Links Management Function in PHP)


I find a need to manage the hyper links in wordpress in a centeralise place. As i have many dofollow links that are no longer valid (accessible). Making those invalid links nofollow is a must-have for Search Engine Optimisation (SEO) purposes – you are responsible to remove dead backlinks to avoid SEO punishment.

In this post, we will be looking at PHP functions to manage the links automatically so that you can either auto expire links or make other links No-Follow in one place. You can use it in your website and it does work well in WordPress as well – which you may need to modify your WordPress Theme’s child function to apply the text/links transformation.

First, the following find_links function takes a HTML string, so you can feed it with the HTML output and it will parse the HTML string and look for each Hyper links:

1
2
3
4
5
6
<?php
function find_links($htmlString){
    $pattern = '/<a (.*?)href=["\'](.*?)\/\/(.*?)["\'](.*?)>(.*?)<\/a>/i';
    $htmlString = preg_replace_callback($pattern, 'process_links', $htmlString);
    return $htmlString;
}
<?php
function find_links($htmlString){
    $pattern = '/<a (.*?)href=["\'](.*?)\/\/(.*?)["\'](.*?)>(.*?)<\/a>/i';
    $htmlString = preg_replace_callback($pattern, 'process_links', $htmlString);
    return $htmlString;
}

As you might notice, the regex is calling back the process_links for each hyperlink it has found. And in the following, you can add your site domain in the $good_domains array. And you can set expiring domains and their corresponding expiry dates so that the links will automatically become NOFOLLOW.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
function process_links($m){
    $good_domains = array(
      "helloacm.com"
    );
    
    $expiring_domains = array(
        "fakedomain.com" => "2099-01-01"
    );
    
    $nofollow = "rel=\"nofollow\"";
    $target = "target=\"blank\"";
    
    $m[2] = strtolower($m[2]);
    if (($m[2] != "https:") && ($m[2] != "http:")) {
        $m[2] = "";
    }
    
    //$domain = str_lower(parse_url($m[3], PHP_URL_HOST));
    $domain = getDomainName($m[3]);
    $curDomain = $_SERVER['SERVER_NAME'] ?? "";
    
    if (in_array($domain, $good_domains) || ($curDomain == $domain)) {
        $nofollow = "";
    }
    
    if ($curDomain == $domain) {
        $target = "";
    }       
    
    if (in_array($domain, array_keys($expiring_domains))) {
        $expiry = $expiring_domains[$domain];
        $today = date("Y-m-d"); 
        if ($today < $expiry) {
            $nofollow = "";
        }        
    }
                                                                    
    return "<a $nofollow $target href=\"{$m[2]}//{$m[3]}\">{$m[5]}</a>";
}
<?php
function process_links($m){
    $good_domains = array(
      "helloacm.com"
    );
    
    $expiring_domains = array(
        "fakedomain.com" => "2099-01-01"
    );
    
    $nofollow = "rel=\"nofollow\"";
    $target = "target=\"blank\"";
    
    $m[2] = strtolower($m[2]);
    if (($m[2] != "https:") && ($m[2] != "http:")) {
        $m[2] = "";
    }
    
    //$domain = str_lower(parse_url($m[3], PHP_URL_HOST));
    $domain = getDomainName($m[3]);
    $curDomain = $_SERVER['SERVER_NAME'] ?? "";
    
    if (in_array($domain, $good_domains) || ($curDomain == $domain)) {
        $nofollow = "";
    }
    
    if ($curDomain == $domain) {
        $target = "";
    }       
    
    if (in_array($domain, array_keys($expiring_domains))) {
        $expiry = $expiring_domains[$domain];
        $today = date("Y-m-d"); 
        if ($today < $expiry) {
            $nofollow = "";
        }        
    }
                                                                    
    return "<a $nofollow $target href=\"{$m[2]}//{$m[3]}\">{$m[5]}</a>";
}

The function changes two properties of a hyper link: _target which will be _blank when it is external domain, and rel which will be set to nofollow automatically. You can also add logics to uniform all URLs e.g. HTTPS auto rewrites, hyper link labels etc.

what-are-bad-links-seo How to Expire DoFollow Links Automatically (SEO Links Management Function in PHP) php SEO

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
498 words
Last Post: Teaching Kids Programming - Converting Spreadsheet Column Titles to Number
Next Post: Teaching Kids Programming - Generate Prime Numbers using Sieve of Eratosthenes Algorithms

The Permanent URL is: How to Expire DoFollow Links Automatically (SEO Links Management Function in PHP)

Leave a Reply