One Snippet That Shuts Down a Mirror of Your Site

A site mirrored by someone else

Sooner or later you notice it: you search for your site's name, the results clearly describe your content, but the domain is one you've never seen. At that point it's safe to assume your site has been mirrored. But what is site mirroring, exactly?

A malicious mirror — also called malicious cloning or malicious resolution — is when someone points a domain's DNS A record straight at your server's IP, so visitors end up on a site identical to yours. Here's roughly how it works: when someone visits the mirror, the script on that server queries your real site for its data, rewrites the links, and serves the result. It's still reading the original site's content. Put more rigorously, it's copying a whole site or part of its pages onto a different domain and server so search engines index the same site or page multiple times.

In plain terms, someone with a domain that carries a bit of authority copies your site by whatever means, keeps everything but the domain identical, and visitors can't tell the difference. On newer sites, even search engines get confused about which copy is real — your genuine site can get dropped from the index while the pirated one gets the rankings.

We don't know what a mirror operator is after, but it never helps you. If that domain hosts anything shady, your mirrored site can end up tainted along with it. This is worth staying alert to.

How do you deal with a mirror? It looks like a complete standalone site, but on every visit the script fetches data from the real site, rewrites the links, and serves it back — it's still reading your data. Below are several fixes compiled by Long Xiaotian; pick whichever fits.

Method 1: Find the mirror's host IP and block it

This walkthrough assumes WordPress. For other systems, test it yourself.

1. Get the mirror server's IP. Note: this may not be the IP that pinging its domain returns. Copy the code below, save it as ip.php, and upload it to your site root.

<?php
$file = "ip.txt"; // name of the log file
$ip = $_SERVER['REMOTE_ADDR'];
$handle = fopen($file, 'a');
fwrite($handle, "IP Address:");
fwrite($handle, "$ip");
fwrite($handle, "\n");
fclose($handele);
?>

2. Then visit the mirror with .../ip.php appended to the URL. An ip.txt file shows up in your site root — open it and copy the IP inside.

3. Open your .htaccess file and append the following, swapping in the IP you just captured:

# Add the IP blacklist
Order Deny,Allow
Deny from 192.168.0.1

If you're behind a CDN, you can also add the IP to its blacklist straight from the CDN dashboard.

Now reload the mirror — a 403? That mirror is handled. From here you just wait for the search crawlers to drop it.

The catch: if the mirror rotates its IP, your block stops working.

Method 2: Block it with JavaScript

Add this JS inside <head></head>:

<script type="text/javascript">
if (document.location.host != "xiaaijun.com") {
location.href = location.href.replace(document.location.host,'xiaaijun.com');
}
</script>

Or this:

<script type="text/javascript">
rthost = window.location.host;
if (rthost != "xiaaijun.com") {
top.location.href = "https://xiaaijun.com";
}
</script>

Replace xiaaijun.com in the code with your site's main home address.

Note: testing shows this fails if the mirror strips JavaScript. Best used together with method 3.

Method 3: Keep working even when JS is stripped

Add this to your header.php:

<div style="display:none;">
<script>aijun2020 = false;</script>
<img src="" onerror='setTimeout(function(){if(typeof(aijun2020)=="undefined"){window.location.host="xiaaijun.com";}},3000);'>
</div>

Some sites filter out JavaScript blocks like <script>...</script>, which removes <script>aijun2020 = false;</script>. The img's onerror fires after a 3000 ms timeout and runs the function: it checks whether aijun2020 still exists, and if it doesn't, rewrites the host to xiaaijun.com. For extra safety, obfuscate the JS (Chinaz JS obfuscator).

Here's this site's obfuscated version:

<div style="display:none;">
<script>aijun2020 = false;</script>
<img src=" " onerror='setTimeout(function(){if(typeof(aijun2020)=="undefined"){window["\x6c\x6f\x63\x61\x74\x69\x6f\x6e"]["\x68\x6f\x73\x74"]="\x78\x69\x61\x61\x69\x6a\x75\x6e\x2e\x63\x6f\x6d";}},3000);'>
</div>
Tested on Chrome, IE11, and 360 Speed Browser: it works, redirecting to the original article page on your site. After changing the img src to a space or an invalid image URL, it works in Firefox too.

Method 4: Lean on img's onerror event

Split the domain string so the mirror can't swap the whole thing, then use an empty img src to trigger onerror, run a JS comparison, and bounce visitors back to the source site when it fails.

① WordPress version

Code below; paste it before the last ?> in your theme's functions.php.

add_action('wp_footer','deny_mirrored_websites');
function deny_mirrored_websites(){
    $currentDomain = "xiaaijun." + "com';
    echo '<img style="display:none" src=" " onerror=\'this.onerror=null;var str1="'.$currentDomain.'";str2="docu"+"ment.loca"+"tion.host";str3=eval(str2);if( str1!=str3 && str3!="cache.baiducontent.com" && str3!="webcache.googleusercontent.com" && str3!="c.360webcache.com" && str3!="cncc.bingj.com" && str3!="snapshot.sogoucdn.com" ){ do_action = "loca" + "tion." + "href = loca" + "tion.href" + ".rep" + "lace(docu" +"ment"+".loca"+"tion.ho"+"st," + "\"' . $currentDomain .'\"" + ")";eval(do_action) }\' />';
}
Ps: I first tried this in wp_head. Testing showed the browser "corrects" an image inside head by shoving it — and things like style.css along with it — into the body, so I moved it to the footer. An image in head isn't really allowed by the spec anyway.

Or:

add_action('wp_footer','deny_mirrored_websites');
function deny_mirrored_websites(){
    $currentDomain = "xiaaijun.' + 'com";
    echo '<img style="display:none" src="nothing" onerror="this.onerror=null;var str1=\''.$currentDomain.'\';str2=\'docu\'+\'ment.loca\'+\'tion.host\';str3=eval(str2);if( str1!=str3 ){ do_action = \'loca\' + \'tion.\' + \'href = loca\' + \'tion.href\' + \'.rep\' + \'lace(docu\' +\'ment\'+\'.loca\'+\'tion.ho\'+\'st,\' + \'\\\'' . $currentDomain .'\\\'\' + \')\';eval(do_action) }" />';
}

Or:

add_action('wp_footer','kimsom_reverse_proxy_defense', 99);
function kimsom_reverse_proxy_defense(){
    $currentDomain = '"xiaaijun" + ".com"';
    echo '<img style="display:none" id="inlojv-rpd" src="nothing" data-url="'.home_url().'" onerror=\'this.onerror=null;var str0=document.getElementById("inlojv-rpd").attributes.getNamedItem("data-url").nodeValue;var ishttps="https:"==document.location.protocol?true:false;if(ishttps){var str1="https"+"://";}else{var str1="http"+"://";}var str2='.$currentDomain.';var str3=str1+str2;if( str0!=str3 ){location.href = location.href.replace(document.location.host,'. $currentDomain .');}\'/>';
}

② Generic HTML version

Since this all runs on JavaScript, any HTML page can use it. The PHP wrapper only exists so it can live in functions.php — as raw HTML it does the same job:

<img style="display:none" src=" " onerror='this.onerror=null;var currentDomain="xiaaijun" + ".com"; var str1=currentDomain; str2="docu"+"ment.loca"+"tion.host"; str3=eval(str2) ;if( str1!=str3 && str3!="cache.baiducontent.com" && str3!="webcache.googleusercontent.com" && str3!="c.360webcache.com" && str3!="cncc.bingj.com" && str3!="snapshot.sogoucdn.com" ){ do_action = "loca" + "tion." + "href = loca" + "tion.href" + ".rep" + "lace(docu" +"ment"+".loca"+"tion.ho"+"st," + "currentDomain" + ")";eval(do_action) }' />

Split the domain in var currentDomain="xiaaijun" + ".com"; into your own pieces the same way, so mirror tooling can't replace the string wholesale — for example var currentDomain="xiaaijun." + "com";.

Then drop the code right after the site's <body> tag (not inside <head>, for the reasons above). This version works on any page.

Method 5: Block by User-Agent signature

There's a general PHP version and a WordPress version — see the original author's post:
https://www.ilxtx.com/mirrored-website.html

Friendly advice: use methods 2 and 3 together. Method 4 already bundles both.

Leave a Comment

Comments No Comments

Table of Contents