{"id":765,"date":"2017-11-03T15:37:24","date_gmt":"2017-11-03T15:37:24","guid":{"rendered":"https:\/\/chemicloud.com\/kb\/?post_type=article&#038;p=765"},"modified":"2021-01-17T15:03:31","modified_gmt":"2021-01-17T15:03:31","slug":"allow-deny-access-website-using-htaccess","status":"publish","type":"ht_kb","link":"https:\/\/chemicloud.com\/kb\/article\/allow-deny-access-website-using-htaccess\/","title":{"rendered":".htaccess 101 &#8211; Allow or Deny Access to Your Website Using .htaccess"},"content":{"rendered":"<p><b>.htaccess<\/b>\u00a0is a resourceful file that can allow or deny access to your website or a folder or files in the directory in which it is placed by using\u00a0<b>order<\/b>,\u00a0<b>allow<\/b>\u00a0and\u00a0<b>deny<\/b>\u00a0keywords.<\/p>\n<h2 id=\"how-to-allow-access-to-a-single-ip-address-using-htaccess\">How to allow access to a single IP address using .htaccess<\/h2>\n<p>In the following example, we will assume that you want to allow access only to\u00a0<strong>1.2.3.4\u00a0<\/strong>IP address. The code that you will need to add in your .htaccess file is:<\/p>\n<pre># Order Allow, Deny\r\nDeny from All\r\nAllow from 1.2.3.4<\/pre>\n<p><b>Order<\/b>\u00a0keyword here specifies the order in which\u00a0<b>allow<\/b>,\u00a0<b>deny<\/b>\u00a0access would be processed. For the above \u2018<b>Order<\/b>\u2019 statement, the\u00a0<b>Allow<\/b>\u00a0statements would be processed first and then the\u00a0<b>deny<\/b>\u00a0statements would be processed.<\/p>\n<h2 id=\"how-to-deny-access-to-a-single-ip-address-using-htaccess\">How to deny access to a single IP Address using .htaccess<\/h2>\n<p>Let&#8217;s assume that you wish to deny or block access to your website from 1.2.3.4 IP address.<\/p>\n<p>The below lines provide the means to allow access to your website from all users except one with the IP Address: <b>1.2.3.4<\/b><\/p>\n<pre># Order Allow, Deny\r\nDeny from 1.2.3.4\r\nDeny from 1.2.3.5\r\nAllow from All\r\n<\/pre>\n<pre># Order Deny, Allow\r\nDeny from 1.2.3.4\r\nDeny from 1.2.3.5<\/pre>\n<p>If there are multiple IP&#8217;s to which you want to deny access, simply add as many &#8216;Deny from&#8217; rules as needed.<\/p>\n<h2 id=\"how-to-deny-access-to-hidden-files-and-directories\">How to Deny Access to Hidden Files and Directories<\/h2>\n<p>Hidden files and directories (those whose names start with a dot\u00a0<code>.<\/code>) should most, if not all, of the time be secured. For example:\u00a0<code>.htaccess<\/code>,\u00a0<code>.htpasswd<\/code>,\u00a0<code>.git<\/code>,\u00a0<code>.hg<\/code>&#8230;<\/p>\n<div class=\"highlight highlight-source-apache-config\">\n<pre><span class=\"pl-c1\">RewriteCond<\/span> <span class=\"pl-c1\">%{SCRIPT_FILENAME}<\/span> <span class=\"pl-s\">-d<\/span> <span class=\"pl-sr\">[OR]<\/span>\r\n<span class=\"pl-c1\">RewriteCond<\/span> <span class=\"pl-c1\">%{SCRIPT_FILENAME}<\/span> <span class=\"pl-s\">-f<\/span>\r\n<span class=\"pl-c1\">RewriteRule<\/span> <span class=\"pl-sr\">\"(^|\/)\\.\"<\/span> <span class=\"pl-s\">-<\/span> <span class=\"pl-sr\">[F]<\/span><\/pre>\n<\/div>\n<p>Alternatively, you can just raise a \u201cNot Found\u201d error, giving the attacker no clue:<\/p>\n<div class=\"highlight highlight-source-apache-config\">\n<pre><span class=\"pl-c1\">RedirectMatch<\/span> 404 \/\\..*$<\/pre>\n<\/div>\n<h2 id=\"deny-access-to-backup-and-source-files\">Deny Access to Backup and Source Files<\/h2>\n<p>These files may be left by some text\/HTML editors (like Vi\/Vim) and pose a great security danger if exposed to the public.<\/p>\n<div class=\"highlight highlight-source-apache-config\">\n<pre>&lt;<span class=\"pl-ent\">FilesMatch<\/span> \"(\\.(bak|config|dist|fla|inc|ini|log|psd|sh|sql|swp)|~)$\"&gt;\r\n    <span class=\"pl-c\">## Apache 2.2<\/span>\r\n    <span class=\"pl-c1\">Order<\/span> allow,deny\r\n    <span class=\"pl-c1\">Deny<\/span> from all\r\n    <span class=\"pl-c1\">Satisfy<\/span> All\r\n\r\n    <span class=\"pl-c\">## Apache 2.4<\/span>\r\n    <span class=\"pl-c\"># Require all denied<\/span>\r\n&lt;\/<span class=\"pl-ent\">FilesMatch<\/span>&gt;<\/pre>\n<\/div>\n<h2 id=\"how-to-disable-directory-browsing\">How to Disable Directory Browsing<\/h2>\n<pre>Options All -Indexes<\/pre>\n<p><strong>That&#8217;s it.<\/strong> Now you know how to Allow or Deny access to your website using .htaccess.<\/p>\n    \t\t<div class=\"hts-messages hts-messages--info    \"   >\r\n    \t\t\t    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\tIf you enjoyed this tutorial, then you\u2019ll love our support! All ChemiCloud\u2019s hosting plans include 24\/7 support from our amazing support team. Check out our <a href=\"https:\/\/chemicloud.com\/web-hosting\/?utm_source=kb&amp;utm_medium=tutorial&amp;utm_content=bottom\" target=\"_blank\" rel=\"noopener\"><strong>web hosting<\/strong><\/a>\u00a0plans and have your website migrated today!    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t\n","protected":false},"excerpt":{"rendered":"<p>.htaccess\u00a0is a resourceful file that can allow or deny access to your website or a folder or files in the directory in which it is placed by using\u00a0order,\u00a0allow\u00a0and\u00a0deny\u00a0keywords. How to allow access to a single IP address using .htaccess In the following example, we will assume that you want to&#8230;<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"ht-kb-category":[34],"ht-kb-tag":[],"class_list":["post-765","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-troubleshooting"],"_links":{"self":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/comments?post=765"}],"version-history":[{"count":8,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/765\/revisions"}],"predecessor-version":[{"id":4411,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/765\/revisions\/4411"}],"wp:attachment":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/media?parent=765"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb-category?post=765"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb-tag?post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}