{"id":3707,"date":"2015-09-05T19:42:05","date_gmt":"2015-09-06T00:42:05","guid":{"rendered":"http:\/\/www.filecloud.com\/blog\/?page_id=3707"},"modified":"2025-01-23T11:52:22","modified_gmt":"2025-01-23T17:52:22","slug":"htaccess-for-beginners","status":"publish","type":"page","link":"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/","title":{"rendered":"htaccess File Type &#8211; Basic and Simple Guide for Beginners"},"content":{"rendered":"<h1>.htaccess for Beginners<\/h1>\n<p dir=\"ltr\">A .htaccess (hypertext access) file is a directory-level plain text configuration file for web servers, which, in simple terms, controls access to a certain directory in your server. The use of .htaccess files became popular because they could be used to override global level server settings related to access of directories. However, in recent times, .htaccess can override many other configuration settings.<\/p>\n<p dir=\"ltr\">Several modern web servers like Apache support .htaccess or related files. Although some other popular servers like Nginx do not have a direct support for .htaccess files, there are ways by which we can convert .htaccess rules to work in Nginx.<\/p>\n<p dir=\"ltr\">.htaccess rules apply to a directory and all its subdirectories, unless there are more .htaccess files present within the sub-directories. The permissions of the .htaccess should be such that it allows universal read access but user only write access.<!--more--><\/p>\n<h2>Advantages and disadvantages:<\/h2>\n<p dir=\"ltr\">.htaccess files are read on every request. Therefore, any changes to these files result in immediate effect, unlike the global settings, which require the server to be restarted. Also, the .htaccess files enables each user to set their permissions for a server with many users.<\/p>\n<p dir=\"ltr\">There is a big catch, though. Since every request requires the web server to read all .htaccess files, it can lead to performance issues if there is a considerable load. Also, decentralizing the settings to different users can lead to security issues, especially if the .htaccess files are not configured properly.<\/p>\n<p dir=\"ltr\">If you want to make changes to a .htaccess file, make sure you keep a backup as a slight syntax error can bring the server down. It is also advisable to put appropriate comments when changing .htaccess files.<\/p>\n<p dir=\"ltr\">Listed here are a few practical things that you can do with .htaccess files.<\/p>\n<h2>Prevent Hotlinking:<\/h2>\n<p dir=\"ltr\">Hotlinking is a process where one web site links directly to an object on another site. The object might be an image, an audio or a video file. Although it doesn\u2019t really hurt for your site\u2019s content to be on another, the downside is a significant amount of your bandwidth that this process eats up!<\/p>\n<p dir=\"ltr\">Any post on .htaccess would be incomplete without mentioning how we can prevent hotlinking using a simple text file! All you need to do is add the following to your .htaccess files.<\/p>\n<p dir=\"ltr\"><em>\u00a0 \u00a0 RewriteEngine on<br \/>\n<\/em><em>\u00a0 \u00a0 RewriteCond %{HTTP_REFERER} !^$<br \/>\n<\/em><em>\u00a0 \u00a0 #domains that can link to your content (images here)<br \/>\n<\/em><em>\u00a0 \u00a0 #add as many as you want<br \/>\n<\/em><em>\u00a0 \u00a0 \u00a0RewriteCond %{HTTP_REFERER} !^http(s)?:\/\/(www\\.)?yoursite.com [NC]<br \/>\n<\/em><em>\u00a0 \u00a0 \u00a0#show no image when hotlinked<br \/>\n<\/em><em>\u00a0 \u00a0 \u00a0RewriteRule \\.(jpg|png|gif)$ \u2013 [NC,F,L]<\/em><\/p>\n<p>Let\u2019s try and understand what this code means. We first turn on the <a href=\"https:\/\/httpd.apache.org\/docs\/current\/mod\/mod_rewrite.html\">RewriteEngine<\/a> so that we can redirect requests to a different URL. <a href=\"https:\/\/httpd.apache.org\/docs\/current\/mod\/mod_rewrite.html#RewriteCond\">RewriteCond<\/a> is a directive that takes in two arguments TestString and CondPattern. ${HTTP_REFERER} is a variable that stores the domain from which the request is coming. We try to match it with a pattern to make sure it\u2019s from your site. The (www\\.)? is to make sure that both the forms www.yoursite.com and yoursite.com are allowed. NC tells the system to ignore casing, F shows a 403 forbidden error \u00a0and L tells the system to stop rewriting.<\/p>\n<p>You can also add an alternate image on hotlinking.<\/p>\n<p dir=\"ltr\">\u00a0\u00a0\u00a0<em>RewriteRule \\.(jpg|png|gif)$ http:\/\/&lt;path_to_your_hotlinked_image&gt; [NC,R,L]<\/em><\/p>\n<p>The R rule here redirects the request.<\/p>\n<h2>Block\/Allowing Users from a certain IP address:<\/h2>\n<p dir=\"ltr\">The .htaccess file\u2019s basic use is to allow or block access to a certain directory. You can configure it to selectively allow or disallow requests from a certain user.<\/p>\n<p dir=\"ltr\">If you find that a spammer has been bothering you, or someone has been trying to scrape your content, you can block their IP address by adding the following-<\/p>\n<p dir=\"ltr\">\u00a0\u00a0\u00a0<em>Order Deny<\/em><br \/>\n<em>\u00a0 \u00a0Deny from 192.168.121.45<\/em><\/p>\n<p dir=\"ltr\">You can also redirect such users to a certain URL.<\/p>\n<p dir=\"ltr\">\u00a0\u00a0\u00a0<em>RewriteCond %{REMOTE_ADDR} 192\\.168\\.121\\.<\/em><br \/>\n<em>\u00a0 \u00a0RewriteRule .* https:\/\/google.com [R,L]<\/em><\/p>\n<p>For security purposes, you might sometimes need to selectively allow only certain IP addresses to a certain location. For instance, you might want only your IP to access the admin area of your site. In such cases, you block requests from all IPs and selectively add your IPs to the whitelist.<\/p>\n<p dir=\"ltr\">\u00a0 \u00a0 <em>order deny,allow<\/em><br \/>\n<em>\u00a0 \u00a0 Deny from all<\/em><br \/>\n<em>\u00a0 \u00a0 # whitelist IP Address 1<\/em><br \/>\n<em>\u00a0 \u00a0 allow from xx.xxx.xx.xx<\/em><br \/>\n<em>\u00a0 \u00a0 #whitelist IP Address 2<\/em><br \/>\n<em>\u00a0 \u00a0 allow from xx.xxx.xx.xx<\/em><\/p>\n<p>The process of blocking and allowing users can also be done using a firewall (like ufw).<\/p>\n<h2>Hiding Directory Listing:<\/h2>\n<p dir=\"ltr\">It might so happen that opening a certain path on the browser lists the directory structure in it (in the absence of an index.html file). For security reasons, it is considered a good idea to restrict this. To make this happen, you just need to add a single line to your .htaccess file.<\/p>\n<p dir=\"ltr\">\u00a0\u00a0\u00a0<em>Options -Indexes<\/em><\/p>\n<p dir=\"ltr\">Doing so shows a Forbidden page when you try to view the directory structure.<\/p>\n<h2>Display Custom Error Pages<\/h2>\n<p dir=\"ltr\">Although this is possible with most of the popular CMS or frameworks, the easiest way to show a custom 404 or 500 error pages are through simple changes in the .htaccess files.<\/p>\n<p dir=\"ltr\"><em>\u00a0\u00a0\u00a0ErrorDocument 404 <\/em><\/p>\n<h2>Force trailing slash<\/h2>\n<p dir=\"ltr\">Search engines treat www.yoursite.com\/something and www.yoursite.com\/something\/ as two different URLs even though they may point to the exact same thing. The <a href=\"http:\/\/googlewebmastercentral.blogspot.in\/2010\/04\/to-slash-or-not-to-slash.html\" class=\"broken_link\">Google Webmasters<\/a> Blog advises that the best way to go about this problem is for the non-slash version to redirect to the slash version. Search engines, thus, would understand that it\u2019s the same content that they are pointing to!<\/p>\n<p dir=\"ltr\">\u00a0\u00a0\u00a0<em>&lt;IfModule mod_rewrite.c&gt;<\/em><br \/>\n<em>\u00a0 \u00a0 RewriteCond %{REQUEST_URI} \/+[^\\.]+$<\/em><br \/>\n<em>\u00a0 \u00a0 RewriteRule ^(.+[^\/])$ %{REQUEST_URI}\/ [R=301,L]<\/em><br \/>\n<em>\u00a0 \u00a0 &lt;\/IfModule&gt;<\/em><\/p>\n<h2>Improving Performance of .htaccess:<\/h2>\n<p dir=\"ltr\">As discussed above, .htaccess files can decrease performance in the case of substantially increased loads. However, this performance dip can be limited by enabling the AllowOverride option in only certain directories. By default, it is enabled for the whole site, and the server checks every directory on every request. You can basically set AllowOverride to None and the allow it for certain directories as show below.<\/p>\n<p dir=\"ltr\">\u00a0 \u00a0 <em># enable allowoverride privileges<\/em><br \/>\n<em>\u00a0 \u00a0 &lt;Directory \/some\/directory\/where\/you\/want\/to\/enable\/AllowOverride&gt;<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0AllowOverride Options<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0&lt;\/Directory&gt;<\/em><\/p>\n<p><a href=\"http:\/\/perishablepress.com\/stupid-htaccess-tricks\/\">Perishable Press<\/a> came up with a huge list of things that you could accomplish with .htaccess, illustrating its power. I would suggest you check that out for further reading.<\/p>\n<p dir=\"ltr\">We hope that this post got you started with .htaccess and served as a launchpad for you to do far more interesting thing with .htacess. Always be careful with .htaccess because of its power- small mistakes regarding this can lead to huge implications!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>.htaccess for Beginners A .htaccess (hypertext access) file is a directory-level plain text configuration file for web servers, which, in simple terms, controls access to a certain directory in your server. The use of .htaccess files became popular because they could be used to override global level server settings related to access of directories. However, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.13 (Yoast SEO v20.13) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>htaccess File Type - Basic and Simple Guide for Beginner&#039;s<\/title>\n<meta name=\"description\" content=\"htaccess File: Beginner&#039;s guide to create a simple and basic directory-level configuration file for webserver that can override global level server configurations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"htaccess File Type - Basic and Simple Guide for Beginners\" \/>\n<meta property=\"og:description\" content=\"htaccess File: Beginner&#039;s guide to create a simple and basic directory-level configuration file for webserver that can override global level server configurations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/\" \/>\n<meta property=\"og:site_name\" content=\"FileCloud blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/tonidopage\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-23T17:52:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.filecloud.com\/blog\/wp-content\/uploads\/2023\/05\/FC-OG-image.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"551\" \/>\n\t<meta property=\"og:image:height\" content=\"289\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@getfilecloud\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/\",\"url\":\"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/\",\"name\":\"htaccess File Type - Basic and Simple Guide for Beginner's\",\"isPartOf\":{\"@id\":\"https:\/\/www.filecloud.com\/blog\/#website\"},\"datePublished\":\"2015-09-06T00:42:05+00:00\",\"dateModified\":\"2025-01-23T17:52:22+00:00\",\"description\":\"htaccess File: Beginner's guide to create a simple and basic directory-level configuration file for webserver that can override global level server configurations.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.filecloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"htaccess File Type &#8211; Basic and Simple Guide for Beginners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.filecloud.com\/blog\/#website\",\"url\":\"https:\/\/www.filecloud.com\/blog\/\",\"name\":\"FileCloud blog\",\"description\":\"Topics on Private cloud, On-Premises, Self-Hosted, Enterprise File Sync and Sharing\",\"publisher\":{\"@id\":\"https:\/\/www.filecloud.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.filecloud.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.filecloud.com\/blog\/#organization\",\"name\":\"FileCloud\",\"url\":\"https:\/\/www.filecloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.filecloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.filecloud.com\/blog\/wp-content\/uploads\/2016\/02\/filecloud_logo_comparison.jpg\",\"contentUrl\":\"https:\/\/www.filecloud.com\/blog\/wp-content\/uploads\/2016\/02\/filecloud_logo_comparison.jpg\",\"width\":155,\"height\":40,\"caption\":\"FileCloud\"},\"image\":{\"@id\":\"https:\/\/www.filecloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/tonidopage\",\"https:\/\/twitter.com\/getfilecloud\",\"https:\/\/www.linkedin.com\/company\/codelathe\",\"https:\/\/www.pinterest.com\/filecloud\/filecloud\/\",\"https:\/\/www.youtube.com\/channel\/UCbU5gTFdNCPESA5aGipFW6g\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"htaccess File Type - Basic and Simple Guide for Beginner's","description":"htaccess File: Beginner's guide to create a simple and basic directory-level configuration file for webserver that can override global level server configurations.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/","og_locale":"en_US","og_type":"article","og_title":"htaccess File Type - Basic and Simple Guide for Beginners","og_description":"htaccess File: Beginner's guide to create a simple and basic directory-level configuration file for webserver that can override global level server configurations.","og_url":"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/","og_site_name":"FileCloud blog","article_publisher":"https:\/\/www.facebook.com\/tonidopage","article_modified_time":"2025-01-23T17:52:22+00:00","og_image":[{"width":551,"height":289,"url":"https:\/\/www.filecloud.com\/blog\/wp-content\/uploads\/2023\/05\/FC-OG-image.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_site":"@getfilecloud","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/","url":"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/","name":"htaccess File Type - Basic and Simple Guide for Beginner's","isPartOf":{"@id":"https:\/\/www.filecloud.com\/blog\/#website"},"datePublished":"2015-09-06T00:42:05+00:00","dateModified":"2025-01-23T17:52:22+00:00","description":"htaccess File: Beginner's guide to create a simple and basic directory-level configuration file for webserver that can override global level server configurations.","breadcrumb":{"@id":"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.filecloud.com\/blog\/htaccess-for-beginners\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.filecloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"htaccess File Type &#8211; Basic and Simple Guide for Beginners"}]},{"@type":"WebSite","@id":"https:\/\/www.filecloud.com\/blog\/#website","url":"https:\/\/www.filecloud.com\/blog\/","name":"FileCloud blog","description":"Topics on Private cloud, On-Premises, Self-Hosted, Enterprise File Sync and Sharing","publisher":{"@id":"https:\/\/www.filecloud.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.filecloud.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.filecloud.com\/blog\/#organization","name":"FileCloud","url":"https:\/\/www.filecloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.filecloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.filecloud.com\/blog\/wp-content\/uploads\/2016\/02\/filecloud_logo_comparison.jpg","contentUrl":"https:\/\/www.filecloud.com\/blog\/wp-content\/uploads\/2016\/02\/filecloud_logo_comparison.jpg","width":155,"height":40,"caption":"FileCloud"},"image":{"@id":"https:\/\/www.filecloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/tonidopage","https:\/\/twitter.com\/getfilecloud","https:\/\/www.linkedin.com\/company\/codelathe","https:\/\/www.pinterest.com\/filecloud\/filecloud\/","https:\/\/www.youtube.com\/channel\/UCbU5gTFdNCPESA5aGipFW6g"]}]}},"_links":{"self":[{"href":"https:\/\/www.filecloud.com\/blog\/wp-json\/wp\/v2\/pages\/3707"}],"collection":[{"href":"https:\/\/www.filecloud.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.filecloud.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.filecloud.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.filecloud.com\/blog\/wp-json\/wp\/v2\/comments?post=3707"}],"version-history":[{"count":19,"href":"https:\/\/www.filecloud.com\/blog\/wp-json\/wp\/v2\/pages\/3707\/revisions"}],"predecessor-version":[{"id":36245,"href":"https:\/\/www.filecloud.com\/blog\/wp-json\/wp\/v2\/pages\/3707\/revisions\/36245"}],"wp:attachment":[{"href":"https:\/\/www.filecloud.com\/blog\/wp-json\/wp\/v2\/media?parent=3707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}