{"id":529,"date":"2019-11-15T02:54:13","date_gmt":"2019-11-15T02:54:13","guid":{"rendered":"http:\/\/blog.hostinger.io\/hostinger-tutorials\/%category\/htaccesshow-to-allow-or-block-visitors-from-specific-countries-using-htaccess\/"},"modified":"2025-12-02T15:33:53","modified_gmt":"2025-12-02T15:33:53","slug":"how-to-block-visitors-from-countries-using-htaccess","status":"publish","type":"post","link":"\/ca\/tutorials\/how-to-block-visitors-from-countries-using-htaccess\/","title":{"rendered":"How to block IP by country: restrict access to your website from specific countries using .htaccess"},"content":{"rendered":"<p>This quick and easy tutorial will show you how to control site access based on visitors&rsquo; location. With some tweaks to the <strong>.htacess<\/strong> file, it&rsquo;s possible to allow or block visitors from certain countries. Keep reading!<\/p><figure class=\"wp-block-image size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/How-to-Speed-Up-Your-Website-EN.pdf\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"566\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=1024,h=1024,fit=scale-down\" alt=\"\" class=\"wp-image-69260\"  sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-allowing-or-blocking-visitors-from-specific-countries-using-htaccess\">Allowing or Blocking Visitors From Specific Countries Using .htaccess<\/h2><p>Once launched, a website is available worldwide. However, in many cases, it&rsquo;s necessary to control your site access, especially when you want to stop spammers and hackers from particular countries.<\/p><figure class=\"wp-block-image size-full\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-web-hosting\" href=\"\/ca\/web-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/public\" alt=\"Hostinger web hosting banner\" class=\"wp-image-98604\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>Fortunately, you can use your WordPress <strong>.htaccess<\/strong> file to allow or block visitors from specific countries. Let&rsquo;s learn how to do so.<\/p><h3 class=\"wp-block-heading\" id=\"h-step-1-generate-the-country-s-ip-addresses\">Step 1: Generate the Country&rsquo;s IP Addresses<\/h3><p>Plenty of online tools are available to allow or block visitors according to their country. In this tutorial, we&rsquo;ll be using <strong>IP2Location<\/strong>.<\/p><ol class=\"wp-block-list\">\n<li>Head to the <a href=\"https:\/\/www.ip2location.com\/free\/visitor-blocker\" target=\"_blank\" rel=\"noopener\">IP2Location Firewall IP list tool<\/a>.<\/li>\n\n\n\n<li>Select the country you want to block or allow.<\/li>\n\n\n\n<li>Choose between IPv4 and IPv6 in the <strong>IP Version<\/strong> menu.<\/li>\n\n\n\n<li>Choose the format in the <strong>Output Format<\/strong> menu. The choice depends on whether you want to block or allow visitors from those countries.<\/li>\n\n\n\n<li>Click <strong>Download<\/strong>.<\/li>\n<\/ol><figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/01\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"971\" height=\"191\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-1.png\/public\" alt=\"The interface of IP2Location IP list tool, showing the country, IP version, and output format menus.\" class=\"wp-image-101527\"  sizes=\"auto, (max-width: 971px) 100vw, 971px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"6\">\n<li>The tool will download the Access Control List (ACL) into your computer.<\/li>\n\n\n\n<li>If you are using the <strong>allow<\/strong> function, make sure to insert the line <strong>deny from all&nbsp;<\/strong>above the list of allowed IPs.<\/li>\n<\/ol><h3 class=\"wp-block-heading\" id=\"h-step-2-insert-the-generated-ip-addresses-into-the-htaccess-file\">Step 2: Insert the Generated IP Addresses into the .htaccess File<\/h3><p>Finally, paste the IP addresses of the countries you want to block or allow to <a href=\"\/ca\/tutorials\/locate-and-create-htaccess\/\"><strong>.htaccess<\/strong> file<\/a>. Here&rsquo;s how to do it using Hostinger&rsquo;s hPanel:<\/p><ol class=\"wp-block-list\">\n<li>Go to <strong>Files<\/strong> &rarr; <strong>File Manager<\/strong>.<\/li>\n\n\n\n<li>Select your domain and hit <strong>Go To File Manager<\/strong>.<\/li>\n\n\n\n<li>Navigate to the <strong>public_html<\/strong> folder and double-click the <strong>.htaccess<\/strong> file.<\/li>\n\n\n\n<li>Open the ACL file on your computer, paste it to your .htaccess file, and hit <strong>Save<\/strong>. Your <strong>.htaccess<\/strong> file will look like this:<\/li>\n<\/ol><figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/01\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"904\" height=\"550\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-2.png\/public\" alt=\"An .htaccess file containing an ACL\" class=\"wp-image-101528\"  sizes=\"auto, (max-width: 904px) 100vw, 904px\" \/><\/a><\/figure><p>You can also use an FTP client to edit the <strong>.htaccess<\/strong> file.<\/p><p><div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <strong>Important!<\/strong> If you cannot access your website after inserting the generated IP addresses, check whether your IP address is included on the block list. You can find your IP address via <a href=\"https:\/\/whatismyipaddress.com\/\" target=\"_blank\" rel=\"noopener\">What Is My IP Address<\/a>\n            <\/p><\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>In this tutorial, you&rsquo;ve learned the easy way to block or allow visitors from specific countries. All you need to do is generate the country&rsquo;s IP address via <strong>IP2Location<\/strong>, then insert an ACL into your <strong>.htaccess<\/strong> file.<\/p><p>We hope that this guide was helpful. Let us know in the comments if you have any questions.<\/p><figure class=\"wp-block-image size-full\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-web-hosting\" href=\"\/ca\/web-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/public\" alt=\"Hostinger web hosting banner\" class=\"wp-image-98604\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>This quick and easy tutorial will show you how to control site access based on visitors&rsquo; location. With some tweaks to the .htacess file, it&rsquo;s possible to allow or block visitors from certain countries. Keep reading! Allowing or Blocking Visitors From Specific Countries Using .htaccess Once launched, a website is available worldwide. However, in many [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ca\/tutorials\/how-to-block-visitors-from-countries-using-htaccess\/\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Block IP by Country Using .htaccess File","rank_math_description":"Control your site access by allowing or blocking visitors from specific countries. Our article will show you how to do it!","rank_math_focus_keyword":"block ip by country","footnotes":""},"categories":[22683,22685],"tags":[],"class_list":["post-529","post","type-post","status-publish","format-standard","hentry","category-hosting","category-technical-aspects"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-block-visitors-from-countries-using-htaccess","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/como-bloquear-acessos-de-ip-via-htaccess","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/comment-bloquer-une-adresse-ip","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/como-bloquear-una-direccion-ip","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/cara-block-ip","default":0},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-block-visitors-from-countries-using-htaccess","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/como-bloquear-una-direccion-ip","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/como-bloquear-una-direccion-ip","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/como-bloquear-una-direccion-ip","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-bloquear-acessos-de-ip-via-htaccess","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-block-visitors-from-countries-using-htaccess","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-block-visitors-from-countries-using-htaccess","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-block-visitors-from-countries-using-htaccess","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-block-visitors-from-countries-using-htaccess","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-block-visitors-from-countries-using-htaccess","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-block-visitors-from-countries-using-htaccess","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/comments?post=529"}],"version-history":[{"count":24,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":138824,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/529\/revisions\/138824"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/media?parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/categories?post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/tags?post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}