Results 1 to 5 of 5

Thread: How to block a country in wordpress?

Hybrid View

  1. #1

    How to block a country in wordpress?

    Seems like my blogs are favorites of the Chinese.

    So how do I block China from my blogs?

  2. #2
    I had to block much of China on my server using the firewall. Don't do it with a plugin. Using a plugin is much slower than using a firewall or the .htaccess file.

    You can block IPs using the .htaccess file, but China has so many IP addresses that it will negatively impact your site's performance. It takes time to check the IP every time a page is requested.

    If you are getting hit with comment spam. About 95% originates in China, regardless of the spam type. WordPress does store the IP address for each comment. Unfortunately, you can only see the IPs if you use phpMyAdmin or another database manager.

    If you want to try blocking all of China, here is how to do it using the .htaccess file.

    Block China Web Traffic IP Addresses and Chinese Hackers

    My recommendation is to identify the specific IPs involved in the attack and only block those. Almost all will be coming from China's university cities.

    The web could be greatly improved if ALL Chinese IP were blocked on the Internet backbone. There is zero value in this traffic and most hacks and malicious email could be stopped.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin

  3. #3
    You could use blockscript, the free version will still redirect any country specified even after the trial period expires, there is a worpress plugin version I've used myself in the past, don't know how or if it'll impact site performance though.

    Or you could try the 'proxy' method using maxmind geoip, I've just tried this on a wordpress site and it seems to work for me, but try at your own risk and make backup copies of any files changed of course

    First upload the maxmind files, GeoIP.dat + (zip file attached or download both from into your domain root folder.

    Then edit your /index.php file in the sites root folder and add this code at the beginning of the file, underneath the the "<?php" part:
    include(''); //this file must exist in your directory
    $gi = geoip_open('GeoIP.dat',GEOIP_STANDARD);
    // get the ip of the visitor
    $addr = getenv('REMOTE_ADDR');
    // translate ip to a country
    $country = geoip_country_name_by_addr($gi, $addr);
    // close the geo database
    $badcountry = array("China", "Anonymous Proxy", "Satellite Provider", "Other");
    // redirect them if country in above string
    if(in_array($country, $badcountry))
    header('Location:'); //enter a url or page on your site
    It will redirect all Chinese (and anonymous) traffic to, though you can change the redirect url to one of your choosing, just edit. You may have to add the code again after any WP updates if they replace the index file, but it is currently installed on and that still works. I tried redirecting UK traffic (where I am) and it did the job, so I took that bit out and left the rest. You can also add any other country you need or want to block as well just by editing the file and adding the country name in quotes, seperated by commas.

    I can't say if this will impact site performance either tbh, I have no way to measure it, but if you get a lot of Chinese traffic and it stops that then overall site performance may well improve because of it, plus it'll save a heap of bandwidth.

    There's a couple of other plugins I use too, one is Bad Behavior which keeps out bad bots, the other is WP-Copyright-Protection which disables any right click copy'n paste efforts.

    Edit: Not sure it'll stop specific pages loading, maybe there's a better place to put the code to work on all pages, I did try the template header.php but that didn't work, but then I'm not a wordpress kinda guy, perhaps someone else has a better idea for it.

    'Nother edit: Just tried it with different pages and it seems to redirect those as well, so could be an easy solution.
    Attached Files Attached Files
    If I can't be a good example, I'll just have to be a terrible warning...

  4. #4
    Thought so.
    Bottom line is that it is a pain in the butt.

    Seems the china traffic is more for the pictures and graphics than anything else.
    Checked and nothing is hotlinked.
    But page specific urls are used.

    Will continue on.

    Thank you for your response.

  5. #5
    Will try these and see what happens.

    Thank you.

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts