{"id":805,"date":"2022-06-02T12:34:56","date_gmt":"2022-06-02T12:34:56","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=805"},"modified":"2023-08-07T09:05:05","modified_gmt":"2023-08-07T09:05:05","slug":"htaccess-file","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/htaccess-file","title":{"rendered":"htaccess file configuration on apache"},"content":{"rendered":"<p>The <strong>htaccess file <\/strong>(hypertext access file) is a powerful tool that allows us to make configurations at the <strong>directory<\/strong> and <strong>subdirectory<\/strong> level and controls how a web server responds to incoming requests.<\/p>\n<p>The main use cases of .htaccess file are:<\/p>\n<ul>\n<li>Setting up URL rewriting rules.<\/li>\n<li>Protecting web pages with passwords.<\/li>\n<li>Displaying custom error pages.<\/li>\n<\/ul>\n<h3>1) Protecting web pages with password<\/h3>\n<ul>\n<li>Open the default Apache configuration file. Use the command that follows.<\/li>\n<\/ul>\n<pre>sudo vi \/etc\/httpd\/conf\/httpd.conf\r\n<\/pre>\n<ul>\n<li>Search directory block for \/var\/www\/html and change <strong>AllowOverride<\/strong> to <strong>AuthConfig<\/strong>\u00a0from <strong>none<\/strong>.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-813\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-06-01-025500-300x160.png\" alt=\"\" width=\"744\" height=\"397\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-06-01-025500-300x160.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-06-01-025500-1024x545.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-06-01-025500-768x409.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-06-01-025500-1200x639.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-06-01-025500.png 1303w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Create .htaccess file in \/var\/www\/html directory.<\/li>\n<\/ul>\n<pre>sudo vi \/var\/www\/html\/.htaccess\r\n<\/pre>\n<ul>\n<li>In the file, paste the following code.<\/li>\n<\/ul>\n<pre><code>AuthType Basic\r\nAuthName \"Restricted Content\"\r\nAuthUserFile \/etc\/httpd\/.htpasswd\r\nRequire valid-user<\/code><\/pre>\n<ul>\n<li>Restart Apache service.<\/li>\n<\/ul>\n<pre>sudo systemctl restart httpd\r\n<\/pre>\n<ul>\n<li>Create .htpasswd file.<\/li>\n<\/ul>\n<pre><span class=\"token function\">sudo<\/span> htpasswd -c \/etc\/apache2\/.htpasswd username\r\n<\/pre>\n<ul>\n<li>Enter password when prompted.<\/li>\n<li>We are all set up. Now go to your browser and access your site, you will see a prompt for username and password.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-814\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-06-01-025626-300x192.png\" alt=\"htaccess\" width=\"730\" height=\"467\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-06-01-025626-300x192.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-06-01-025626.png 568w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<h3>2) Setting up URL rewriting rules<\/h3>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>htaccess file can be used to transfer requests from one directory to other. In the file, paste the following code.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>Options +FollowSymLinks\r\nRewriteEngine On\r\nRewriteRule ^folder1.*$ http:\/\/example.com\/folder2\/ [R=301,L]<\/pre>\n<ul>\n<li>Copy the following code, if you want to stop traffic from being redirected from a particular site to yours.<\/li>\n<\/ul>\n<pre>RewriteEngine on\r\n# Options +FollowSymlinks\r\nRewriteCond %{HTTP_REFERER} blockedsite\\.com [NC]\r\nRewriteRule .* - [F]\r\n<\/pre>\n<h3>3)\u00a0 Setting up custom error page<\/h3>\n<ul>\n<li>Add the following code in file.<\/li>\n<\/ul>\n<pre>ErrorDocument 404 \/errorpage.html\r\n<\/pre>\n<ul>\n<li>Make a custom error html page in the directory with htaccess file.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.checkmateq.com\/contact-us\">Please contact<\/a> our technical consultants if you have anything related to SRE Management, Cloud infrastructure and DevOps Management. We can also help to hire remote <a href=\"https:\/\/www.checkmateq.com\/full-stack-development\">fullStack developer<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The htaccess file (hypertext access file) is a powerful tool that allows us to make configurations at the directory and subdirectory level and controls how a web server responds to incoming requests. The main use cases of .htaccess file are: Setting up URL rewriting rules. Protecting web pages with passwords. Displaying custom error pages. 1) &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/htaccess-file\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;htaccess file configuration on apache&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":770,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,2,7,8,6],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/805"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=805"}],"version-history":[{"count":21,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/805\/revisions"}],"predecessor-version":[{"id":4330,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/805\/revisions\/4330"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/770"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}