{"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":"2026-03-09T19:19:24","modified_gmt":"2026-03-09T19:19:24","slug":"how-to-block-visitors-from-countries-using-htaccess","status":"publish","type":"post","link":"\/ng\/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":"<?xml encoding=\"utf-8\" ?><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\"><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\/public\" alt=\"\" class=\"wp-image-69260\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=768,fit=scale-down 768w\" 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><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-full\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-web-hosting\" href=\"\/ng\/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\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=768,fit=scale-down 768w\" 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\">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\/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\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-1.png\/w=971,fit=scale-down 971w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-1.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-1.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-1.png\/w=768,fit=scale-down 768w\" 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=\"\/ng\/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\/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\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-2.png\/w=904,fit=scale-down 904w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-2.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-2.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/01\/image-2.png\/w=768,fit=scale-down 768w\" 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><p class=\"important\"><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\/\">What Is My IP Address<\/a><\/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><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-full\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-web-hosting\" href=\"\/ng\/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\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=768,fit=scale-down 768w\" 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=\"\/ng\/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":[22630],"tags":[],"class_list":["post-529","post","type-post","status-publish","format-standard","hentry","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-UK","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\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/comments?post=529"}],"version-history":[{"count":24,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":144946,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/529\/revisions\/144946"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/media?parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/categories?post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/tags?post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}