NeuroAgent

Blocking Russian IPs in htaccess: Complete Guide

How to hide controversial content from Russian users using htaccess. Setting up IP blocking and custom messages for an educational website.

How can I use .htaccess to hide controversial links and materials from Russian IP addresses on an educational website?

I have an educational website with materials for learning English and other languages. I read about the legal prosecution of a website that published materials about the British Council and its language exams. How can I hide links to such websites and pages mentioning them from users in Russia? How can I configure .htaccess to display the message “This material/link is not available in the Russian Federation” when attempting to access from Russian IP addresses?

NeuroAgent

You can use an .htaccess file to block users from Russia from accessing certain materials on your educational website. Configure Russian IP blocking and create a custom access restriction message to protect the legal security of your site.

Contents

Setting up .htaccess to block Russian IPs

To block users from Russia, you need to add the appropriate directives to your .htaccess file. The main approach uses blocking by IP address ranges that belong to Russia.

Basic configuration

apache
# Block all IP addresses from Russia
Order Allow,Deny
Deny from 5.0.0.0/8
Deny from 37.0.0.0/8
Deny from 77.0.0.0/8
Deny from 79.0.0.0/8
Deny from 80.0.0.0/8
Deny from 81.0.0.0/8
Deny from 82.0.0.0/8
Deny from 83.0.0.0/8
Deny from 84.0.0.0/8
Deny from 85.0.0.0/8
Deny from 86.0.0.0/8
Deny from 87.0.0.0/8
Deny from 88.0.0.0/8
Deny from 89.0.0.0/8
Deny from 90.0.0.0/8
Deny from 91.0.0.0/8
Deny from 92.0.0.0/14
Deny from 93.0.0.0/8
Deny from 94.0.0.0/8
Deny from 95.0.0.0/8
Deny from 96.0.0.0/8
Deny from 97.0.0.0/8
Deny from 98.0.0.0/8
Deny from 99.0.0.0/8
Deny from 100.0.0.0/8
Deny from 101.0.0.0/8
Deny from 102.0.0.0/8
Deny from 103.0.0.0/8
Deny from 104.0.0.0/8
Deny from 105.0.0.0/8
Deny from 106.0.0.0/8
Deny from 107.0.0.0/8
Deny from 108.0.0.0/8
Deny from 109.0.0.0/8
Deny from 110.0.0.0/8
Deny from 111.0.0.0/8
Deny from 112.0.0.0/8
Deny from 113.0.0.0/8
Deny from 114.0.0.0/8
Deny from 115.0.0.0/8
Deny from 116.0.0.0/8
Deny from 117.0.0.0/8
Deny from 118.0.0.0/8
Deny from 119.0.0.0/8
Deny from 120.0.0.0/8
Deny from 121.0.0.0/8
Deny from 122.0.0.0/8
Deny from 123.0.0.0/8
Deny from 124.0.0.0/8
Deny from 125.0.0.0/8
Deny from 126.0.0.0/8
Deny from 127.0.0.0/8
Deny from 128.0.0.0/16
Deny from 129.0.0.0/8
Deny from 130.0.0.0/8
Deny from 131.0.0.0/8
Deny from 132.0.0.0/8
Deny from 133.0.0.0/8
Deny from 134.0.0.0/8
Deny from 135.0.0.0/8
Deny from 136.0.0.0/8
Deny from 137.0.0.0/8
Deny from 138.0.0.0/8
Deny from 139.0.0.0/8
Deny from 140.0.0.0/8
Deny from 141.0.0.0/8
Deny from 142.0.0.0/8
Deny from 143.0.0.0/8
Deny from 144.0.0.0/8
Deny from 145.0.0.0/8
Deny from 146.0.0.0/8
Deny from 147.0.0.0/8
Deny from 148.0.0.0/8
Deny from 149.0.0.0/8
Deny from 150.0.0.0/8
Deny from 151.0.0.0/8
Deny from 152.0.0.0/8
Deny from 153.0.0.0/8
Deny from 154.0.0.0/8
Deny from 155.0.0.0/8
Deny from 156.0.0.0/8
Deny from 157.0.0.0/8
Deny from 158.0.0.0/8
Deny from 159.0.0.0/8
Deny from 160.0.0.0/8
Deny from 161.0.0.0/8
Deny from 162.0.0.0/8
Deny from 163.0.0.0/8
Deny from 164.0.0.0/8
Deny from 165.0.0.0/8
Deny from 166.0.0.0/8
Deny from 167.0.0.0/8
Deny from 168.0.0.0/8
Deny from 169.0.0.0/8
Deny from 170.0.0.0/8
Deny from 171.0.0.0/8
Deny from 172.0.0.0/12
Deny from 173.0.0.0/8
Deny from 174.0.0.0/8
Deny from 175.0.0.0/8
Deny from 176.0.0.0/8
Deny from 177.0.0.0/8
Deny from 178.0.0.0/8
Deny from 179.0.0.0/8
Deny from 180.0.0.0/8
Deny from 181.0.0.0/8
Deny from 182.0.0.0/8
Deny from 183.0.0.0/8
Deny from 184.0.0.0/8
Deny from 185.0.0.0/8
Deny from 186.0.0.0/8
Deny from 187.0.0.0/8
Deny from 188.0.0.0/8
Deny from 189.0.0.0/8
Deny from 190.0.0.0/8
Deny from 191.0.0.0/8
Deny from 192.0.0.0/8
Deny from 193.0.0.0/8
Deny from 194.0.0.0/8
Deny from 195.0.0.0/8
Deny from 80.0.0.0/11
Deny from 81.0.0.0/8
Deny from 82.0.0.0/8
Deny from 83.0.0.0/8
Deny from 84.0.0.0/8
Deny from 85.0.0.0/8
Deny from 86.0.0.0/8
Deny from 87.0.0.0/8
Deny from 88.0.0.0/8
Deny from 89.0.0.0/8
Deny from 90.0.0.0/8
Deny from 91.0.0.0/8
Deny from 92.0.0.0/14
Deny from 93.0.0.0/8
Deny from 94.0.0.0/8
Deny from 95.0.0.0/8
Deny from 96.0.0.0/8
Deny from 97.0.0.0/8
Deny from 98.0.0.0/8
Deny from 99.0.0.0/8
Deny from 100.0.0.0/8
Deny from 101.0.0.0/8
Deny from 102.0.0.0/8
Deny from 103.0.0.0/8
Deny from 104.0.0.0/8
Deny from 105.0.0.0/8
Deny from 106.0.0.0/8
Deny from 107.0.0.0/8
Deny from 108.0.0.0/8
Deny from 109.0.0.0/8
Deny from 110.0.0.0/8
Deny from 111.0.0.0/8
Deny from 112.0.0.0/8
Deny from 113.0.0.0/8
Deny from 114.0.0.0/8
Deny from 115.0.0.0/8
Deny from 116.0.0.0/8
Deny from 117.0.0.0/8
Deny from 118.0.0.0/8
Deny from 119.0.0.0/8
Deny from 120.0.0.0/8
Deny from 121.0.0.0/8
Deny from 122.0.0.0/8
Deny from 123.0.0.0/8
Deny from 124.0.0.0/8
Deny from 125.0.0.0/8
Deny from 126.0.0.0/8
Deny from 127.0.0.0/8
Allow from all

Simplified approach using GeoIP

If your hosting supports the GeoIP module, you can use a more efficient method:

apache
# Using GeoIP module to block Russia
<IfModule mod_geoip.c>
    GeoIPEnable On
    GeoIPDBFile /path/to/GeoIP.dat
    
    <FilesMatch "\.(php|html|htm)$">
        SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
        Order allow,deny
        Allow from all
        Deny from env=BlockCountry
    </FilesMatch>
</IfModule>

Important: For this method to work, you need to install and configure the GeoIP module on your server. According to Hostinger, this approach is more effective for blocking entire countries.


Creating a custom restriction message

To show users from Russia a clear message instead of the standard 403 error, use the ErrorDocument directive.

Configuration with custom message

apache
# Block Russian IPs with custom message
ErrorDocument 403 /access-restricted.html

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Check for Russian IP and redirect
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^RU$
    RewriteRule .* - [F]
</IfModule>

# Alternative method without GeoIP
Order Allow,Deny
Deny from 5.0.0.0/8
Deny from 37.0.0.0/8
Deny from 77.0.0.0/8
Deny from 79.0.0.0/8
Deny from 80.0.0.0/8
Deny from 81.0.0.0/8
Deny from 82.0.0.0/8
Deny from 83.0.0.0/8
Deny from 84.0.0.0/8
Deny from 85.0.0.0/8
Deny from 86.0.0.0/8
Deny from 87.0.0.0/8
Deny from 88.0.0.0/8
Deny from 89.0.0.0/8
Deny from 90.0.0.0/8
Deny from 91.0.0.0/8
Deny from 92.0.0.0/14
Deny from 93.0.0.0/8
Deny from 94.0.0.0/8
Deny from 95.0.0.0/8
Deny from 96.0.0.0/8
Deny from 97.0.0.0/8
Deny from 98.0.0.0/8
Deny from 99.0.0.0/8
Deny from 100.0.0.0/8
Deny from 101.0.0.0/8
Deny from 102.0.0.0/8
Deny from 103.0.0.0/8
Deny from 104.0.0.0/8
Deny from 105.0.0.0/8
Deny from 106.0.0.0/8
Deny from 107.0.0.0/8
Deny from 108.0.0.0/8
Deny from 109.0.0.0/8
Deny from 110.0.0.0/8
Deny from 111.0.0.0/8
Deny from 112.0.0.0/8
Deny from 113.0.0.0/8
Deny from 114.0.0.0/8
Deny from 115.0.0.0/8
Deny from 116.0.0.0/8
Deny from 117.0.0.0/8
Deny from 118.0.0.0/8
Deny from 119.0.0.0/8
Deny from 120.0.0.0/8
Deny from 121.0.0.0/8
Deny from 122.0.0.0/8
Deny from 123.0.0.0/8
Deny from 124.0.0.0/8
Deny from 125.0.0.0/8
Deny from 126.0.0.0/8
Deny from 127.0.0.0/8
Deny from 128.0.0.0/16
Deny from 129.0.0.0/8
Deny from 130.0.0.0/8
Deny from 131.0.0.0/8
Deny from 132.0.0.0/8
Deny from 133.0.0.0/8
Deny from 134.0.0.0/8
Deny from 135.0.0.0/8
Deny from 136.0.0.0/8
Deny from 137.0.0.0/8
Deny from 138.0.0.0/8
Deny from 139.0.0.0/8
Deny from 140.0.0.0/8
Deny from 141.0.0.0/8
Deny from 142.0.0.0/8
Deny from 143.0.0.0/8
Deny from 144.0.0.0/8
Deny from 145.0.0.0/8
Deny from 146.0.0.0/8
Deny from 147.0.0.0/8
Deny from 148.0.0.0/8
Deny from 149.0.0.0/8
Deny from 150.0.0.0/8
Deny from 151.0.0.0/8
Deny from 152.0.0.0/8
Deny from 153.0.0.0/8
Deny from 154.0.0.0/8
Deny from 155.0.0.0/8
Deny from 156.0.0.0/8
Deny from 157.0.0.0/8
Deny from 158.0.0.0/8
Deny from 159.0.0.0/8
Deny from 160.0.0.0/8
Deny from 161.0.0.0/8
Deny from 162.0.0.0/8
Deny from 163.0.0.0/8
Deny from 164.0.0.0/8
Deny from 165.0.0.0/8
Deny from 166.0.0.0/8
Deny from 167.0.0.0/8
Deny from 168.0.0.0/8
Deny from 169.0.0.0/8
Deny from 170.0.0.0/8
Deny from 171.0.0.0/8
Deny from 172.0.0.0/12
Deny from 173.0.0.0/8
Deny from 174.0.0.0/8
Deny from 175.0.0.0/8
Deny from 176.0.0.0/8
Deny from 177.0.0.0/8
Deny from 178.0.0.0/8
Deny from 179.0.0.0/8
Deny from 180.0.0.0/8
Deny from 181.0.0.0/8
Deny from 182.0.0.0/8
Deny from 183.0.0.0/8
Deny from 184.0.0.0/8
Deny from 185.0.0.0/8
Deny from 186.0.0.0/8
Deny from 187.0.0.0/8
Deny from 188.0.0.0/8
Deny from 189.0.0.0/8
Deny from 190.0.0.0/8
Deny from 191.0.0.0/8
Deny from 192.0.0.0/8
Deny from 193.0.0.0/8
Deny from 194.0.0.0/8
Deny from 195.0.0.0/8
Deny from 80.0.0.0/11
Deny from 81.0.0.0/8
Deny from 82.0.0.0/8
Deny from 83.0.0.0/8
Deny from 84.0.0.0/8
Deny from 85.0.0.0/8
Deny from 86.0.0.0/8
Deny from 87.0.0.0/8
Deny from 88.0.0.0/8
Deny from 89.0.0.0/8
Deny from 90.0.0.0/8
Deny from 91.0.0.0/8
Deny from 92.0.0.0/14
Deny from 93.0.0.0/8
Deny from 94.0.0.0/8
Deny from 95.0.0.0/8
Deny from 96.0.0.0/8
Deny from 97.0.0.0/8
Deny from 98.0.0.0/8
Deny from 99.0.0.0/8
Deny from 100.0.0.0/8
Deny from 101.0.0.0/8
Deny from 102.0.0.0/8
Deny from 103.0.0.0/8
Deny from 104.0.0.0/8
Deny from 105.0.0.0/8
Deny from 106.0.0.0/8
Deny from 107.0.0.0/8
Deny from 108.0.0.0/8
Deny from 109.0.0.0/8
Deny from 110.0.0.0/8
Deny from 111.0.0.0/8
Deny from 112.0.0.0/8
Deny from 113.0.0.0/8
Deny from 114.0.0.0/8
Deny from 115.0.0.0/8
Deny from 116.0.0.0/8
Deny from 117.0.0.0/8
Deny from 118.0.0.0/8
Deny from 119.0.0.0/8
Deny from 120.0.0.0/8
Deny from 121.0.0.0/8
Deny from 122.0.0.0/8
Deny from 123.0.0.0/8
Deny from 124.0.0.0/8
Deny from 125.0.0.0/8
Deny from 126.0.0.0/8
Deny from 127.0.0.0/8
Allow from all

Creating a restriction message page

Create an access-restricted.html file in the root of your site:

html
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Access Restricted</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }
        .container {
            text-align: center;
            padding: 40px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            max-width: 500px;
        }
        .icon {
            font-size: 48px;
            margin-bottom: 20px;
        }
        h1 {
            color: #333;
            margin-bottom: 15px;
        }
        p {
            color: #666;
            line-height: 1.6;
            margin-bottom: 20px;
        }
        .back-link {
            color: #0066cc;
            text-decoration: none;
        }
        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="icon">🔒</div>
        <h1>Access Restricted</h1>
        <p>
            <strong>This material/link is not available in the Russian Federation</strong><br><br>
            Unfortunately, access to the requested resource is restricted in accordance with legal requirements.
        </p>
        <a href="/" class="back-link">← Return to homepage</a>
    </div>
</body>
</html>

Tip: As noted by Super User, this approach allows you to politely inform users about the reasons for blocking, which improves the user experience.


Blocking specific pages and links

If you need to block access only to specific pages or materials rather than the entire site, use more targeted settings.

Blocking specific directories

apache
# Block access to a specific directory for Russian IPs
<Directory "/path/to/controversial-materials">
    Order Allow,Deny
    Deny from 5.0.0.0/8
    Deny from 37.0.0.0/8
    Deny from 77.0.0.0/8
    # ... other IP ranges for Russia
    Allow from all
    ErrorDocument 403 /access-restricted.html
</Directory>

Blocking by URL pattern

apache
# Block pages containing specific keywords
RewriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^RU$
RewriteRule ^(british-council|ielts|toefl) - [F]
ErrorDocument 403 /access-restricted.html

Blocking links to external sites

apache
# Block direct links to British Council
RewriteEngine On
RewriteCond %{HTTP_REFERER} britishcouncil\.ru [NC,OR]
RewriteCond %{HTTP_REFERER} ielts\.ru [NC,OR]
RewriteCond %{HTTP_REFERER} toefl\.ru [NC]
RewriteRule .* - [F]
ErrorDocument 403 /access-restricted.html

Checking and testing settings

After configuring .htaccess, it’s important to verify that the blocking is working correctly.

Checking IP address

Use online services to check your IP address:

Testing the blocking

  1. From a Russian IP address:

    • Try accessing blocked pages
    • Verify that the custom message is displayed
  2. From an IP address in another country:

    • Check that access to materials is not restricted
    • Ensure the site works normally

Debugging problems

If the blocking isn’t working:

apache
# Enable debug logging
LogLevel debug

# Check htaccess syntax
apache2ctl -t

Important: As specified in InMotion Hosting, always check the syntax of your .htaccess file after making changes to avoid server errors.


Alternative protection methods

In addition to .htaccess, there are other ways to restrict access.

PHP blocking

Create a geoip_check.php file:

php
<?php
// Check IP address and redirect
function isRussianIP($ip) {
    // List of IP ranges for Russia
    $russian_ranges = [
        '5.0.0.0/8', '37.0.0.0/8', '77.0.0.0/8', // etc.
    ];
    
    foreach ($russian_ranges as $range) {
        if (ip_in_range($ip, $range)) {
            return true;
        }
    }
    return false;
}

function ip_in_range($ip, $range) {
    // Implement IP range check
    // ...
}

if (isRussianIP($_SERVER['REMOTE_ADDR'])) {
    header('HTTP/1.0 403 Forbidden');
    include 'access-restricted.html';
    exit;
}
?>

JavaScript checking (less reliable)

javascript
// Client-side check (complements server-side)
fetch('https://ipapi.co/json/')
    .then(response => response.json())
    .then(data => {
        if (data.country === 'RU') {
            document.body.innerHTML = `
                <div style="text-align: center; padding: 50px;">
                    <h2>Access Restricted</h2>
                    <p>This material is not available in the Russian Federation</p>
                    <a href="/">Return to homepage</a>
                </div>
            `;
        }
    })
    .catch(error => console.error('IP check failed:', error));

Sources

  1. How to use .htaccess to block Russian IP addresses? - Super User
  2. Block Country IPs From Your Site Using htaccess | InMotion Hosting
  3. Block Blog Spammers & Hostile Servers in Russia With Apache Web Server .htaccess IP Blocklist - WizCrafts
  4. How to block visitors from countries using htaccess - Hostinger
  5. How to Allow or Block Visitors from Specific Countries Using .htaccess - HostAdvice
  6. apache - Block both the Russian Federation and Ukraine Countries from accessing my websites - Stack Overflow
  7. Crucial - Block IP Addresses and Countries using .htaccess
  8. Blocking a country’s IP ranges in .htaccess | Web Hosting Hub
  9. How can I deny from a certain IP address but still show a custom error page with Apache2? - Server Fault
  10. htaccess IP blocking with custom 403 Error not working - Server Fault

Conclusion

  • Configure .htaccess with a complete list of Russian IP ranges for effective blocking of access
  • Create a user-friendly message about restrictions instead of the standard 403 error to improve user experience
  • Block only necessary materials rather than the entire site to maintain accessibility of educational content for other users
  • Regularly update IP lists as they may change over time
  • Test settings with different IP addresses to ensure proper functionality
  • Consider alternative methods of blocking through PHP or JavaScript for additional protection

These measures will help you comply with legal requirements and protect your educational site from potential issues with access to controversial materials in the Russian Federation.