Restrict site access by country (geoblock)

Sometimes it may be useful or required to restrict or redirect a visitor's access to a site based on what country the visitor is located in. Seravo has a feature integrated to do these restrictions, and here's how to configure it.

Restrict access with PHP

Using the Seravo-specific HTTP header X-Seravo-Country-Code you can achieve more specific restrictions.

Forward visitors to another site based on country code

Useful e.g. if you have a multilingual site and you want to forward visitors directly to the correct language version.

<?php
if (isset($_SERVER['HTTP_X_SERAVO_COUNTRY_CODE'])) {
    switch (strtolower($_SERVER['HTTP_X_SERAVO_COUNTRY_CODE'])) {
        
        // Forward Finnish visitors to example.fi
        case 'fi':
            header("Location: https://example.fi");
            exit();
        
        // Forward Swedish visitors to example.com
        case 'se':
            header("Location: https://example.com");
            exit();
            
        default:
        
            // Do nothing, continue processing in WordPress
            break;
    }
} else {
    error_log("Error: X-Seravo-Country-Code header not found!");
}

Login page restrictions with WP-CLI

It is also possible to set a location-based restriction for the WordPress login page. This feature is currently being implemented. Meanwhile, we recommend installing a reCaptcha plugin for login pages and making use of two-factor authentication (2FA) when logging in.

Further reading

Read more on restricting access on your WordPress website from Seravo's Developer Docs.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.