{"id":2328,"date":"2018-02-17T18:43:31","date_gmt":"2018-02-17T13:13:31","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=2328"},"modified":"2026-09-18T17:33:53","modified_gmt":"2026-09-18T12:03:53","slug":"block-wp-login-php-based-country-mod_security","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/block-wp-login-php-based-country-mod_security\/","title":{"rendered":"Block wp-login.php based on country through mod_security"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">WordPress websites commonly receive repeated and automated login attempts targeting the <code>wp-login.php<\/code> page. To reduce unwanted login traffic and improve website security, ModSecurity can be configured to restrict access to the WordPress login page based on the visitor&#8217;s country. This allows website administrators to permit login access only from trusted countries while blocking requests originating from other locations. In this example, access to <code>wp-login.php<\/code> is restricted to users from India, while requests from other countries are blocked with a <code>406 Not Acceptable<\/code> response.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ModSecurity must be installed and enabled on the Apache server. GeoIP\/GeoLocation support must be available and configured for ModSecurity. Root or administrative SSH access to the server is required. Apache configuration files must be accessible for modification. A valid ModSecurity rule ID should be used to avoid conflicts with existing rules. Take a backup of the Apache\/ModSecurity configuration before making changes.<\/p>\n\n\n<h4>Implementation:<\/h4>\n<p>1. Open the domain configuration file. By default, the location will be \/etc\/apache2\/sites-enabled\/default<\/p>\n<p><code>vi \/etc\/apache2\/sites-enabled\/default<\/code><\/p>\n<p>2. Append the code below<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;IfModule\u00a0mod_security2.c&gt;\nSecRule REQUEST_URI \"wp-login.php\" \"chain,id:2018,log,msg:'Blocking %{geo.country_code}' - Suggested by PheonixSolutions.com\"\nSecRule REMOTE_ADDR \"@geoLookup\" \"chain\"\nSecRule GEO:COUNTRY_CODE \"@streq IN\"\n&lt;\/IfModule&gt;<\/pre>\n<p>Where,<\/p>\n<p>Secrule &#8211; ModSecurity rule<\/p>\n<p>Request_URI &#8211; URL to which the rules apply<\/p>\n<p>chain &#8211; It indicates that there will be continuation of the rule to the next line.<\/p>\n<p>id &#8211; Unique Mod_security reference ID<\/p>\n<p>log- It tells Apache to log the message<\/p>\n<p>msg &#8211; Message should be logged in the log<\/p>\n<p>GEO:COUNTRY_CODE &#8211; Country code<\/p>\n<p>streq &#8211; String Equal to<\/p>\n<p>IN &#8211; India. If we want to allow access from other countries, then mention the two-digit code of the respective country<\/p>\n<p>3. Check the syntax error<\/p>\n<p><code>apachectl -t<\/code><\/p>\n<p>4. Restart the Apache server.<\/p>\n<p><code>service apache2 restart<\/code><\/p>\n<p>Now, if any user accesses wp-login.php from India, they will see the login page. If the user accesses wp-login.php, then they will get a &#8220;<strong>406: Not acceptable &#8220;<\/strong> page.<\/p>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using ModSecurity to restrict access to the WordPress <code>wp-login.php<\/code> page based on the visitor&#8217;s country provides an additional layer of security against unwanted and automated login attempts. In this configuration, only users from India are allowed to access the login page, while requests from other countries are blocked with a <code>406 Not Acceptable<\/code> response. The rule can be customized to allow access from other required countries based on the website&#8217;s business and security requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. Can I allow access to <code>wp-login.php<\/code> from multiple countries?<\/strong><br>Yes. The ModSecurity rule can be modified to allow multiple country codes based on the website&#8217;s requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. What happens when a user from a blocked country accesses <code>wp-login.php<\/code>?<\/strong><br>The request is blocked by ModSecurity, and the user receives a <code>406 Not Acceptable<\/code> response.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. Can I change India to another country?<\/strong><br>Yes. Replace the <code>IN<\/code> country code in the ModSecurity rule with the required two-letter country code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/installing-modsecurity-with-owasp-crs-on-apache-ubuntu\/\">Installing ModSecurity with OWASP CRS on Apache (Ubuntu)<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business?. Our experts can help with\u00a0web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team\u00a0<a href=\"https:\/\/pheonixsolutions.com\/contact\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction WordPress websites commonly receive repeated and automated login attempts targeting the wp-login.php page. To reduce unwanted login traffic and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[359,307,167],"class_list":["post-2328","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-ddos","tag-security","tag-wordpress-2","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-By","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2328"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2328\/revisions"}],"predecessor-version":[{"id":11843,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2328\/revisions\/11843"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}