How can I make desktop visits return a 404 while the site opens normally on mobile?

来源:知了网络
时间:2026-10-31 20:09:56
浏览:694

Webmasters often tell me that they've come across some unusual high-authority sites — certain film and TV sites, novel sites, and so on — with very high authority and excellent SEO results, and they want to study them. But when they visit from a computer they get a 404, while opening the site on a phone works normally, so they ask me how this is done. This actually has little to do with SEO; it's more of a set of handling techniques on the programming/technical side, and today I'll share it here.

Technical principle:

The operations above essentially work by identifying the requesting IP (some identify the visitor's UA attribute instead) and making a judgment based on it. To avoid affecting SEO results, crawlers are shown one type of page when they visit, while users are served a different set of pages — a technique leaning toward black hat SEO.

Technical code:

<?php 
$allowedIPs = array("127.0.0.1","10.0.0.1");
function getIp(){
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
        $cip = $_SERVER["HTTP_CLIENT_IP"];
    }else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    }else if(!empty($_SERVER["REMOTE_ADDR"])){
        $cip = $_SERVER["REMOTE_ADDR"];
    }else{
        $cip = '''';
    }
    preg_match("/[\d\.]{7,15}/",$cip,$cips);
    $cip = isset($cips[0])?$cips[0]:''unknown'';
    unset($cips);
    return $cip;
}
function isPc(){
    $userAgent = $_SERVER[''HTTP_USER_AGENT''];
    $mobileKeywords= array(''iPhone'', ''Android'', ''Windows Phone'');
    foreach ($mobileKeywords as $keyword) {
        if(stripos($userAgent, $keyword) !== false){
            return false;
        }
        return true;
    }
}
if (isPc() && !in_array(getIp(), $allowedIPs)){
    header("HTTP/1.0 404 Not Found");
    exit();
}
?>

The above is a piece of PHP code that implements this scenario. Just replace the "127.0.0.1","10.0.0.1" inside with the IPs of search engine crawlers, name the file index.php, and place it in the site's root directory. For the similar approach of identifying the UA (UA means user-agent) attribute to make the judgment, try it yourself.

This article is for technical sharing and learning purposes only and must not be used for illegal purposes. 知了网络 does not do black hat SEO and does not provide any technical support for any gray-area or sensitive industries — please do not inquire.

相关阅读
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31
  • 2026-10-31