We use cookies

This site uses cookies from cmlabs to deliver and enhance the quality of its services and to analyze traffic..

Master SEO vocabulary with all essential terms and meanings here.

.htaccess

Last updated: Aug 25, 2023

What is an .htaccess file?

The .htaccess file is a configuration file used to manage the Apache web server. This file is a simple text file written in ASCII format.

Using the .htaccess file, you can set the server configuration for specific directories such as the site's root directory or subdirectories created to enable and disable the functionality of a running website.

 

.htaccess File Functions

The .htaccess file has several important functions for websites, ranging from configuring user permissions, handling errors, resetting URLs, redirecting certain URLs, and so on. The .htaccess function is as follows:

Changing Homepage Defaults

If you want to change the page you want to load first, then you can change the default homepage configuration via the .htaccess file. For example, when you want to change the start page from index.html to homepage.html, then you can change the homepage configuration using the following code:

DirectoryIndex homepage.html

cmlabs

You can also add multiple files in this configuration, so that if the web server doesn't find the file, it checks for the next file.

DirectoryIndex index.html homepage.html config.php

cmlabs

Block Specific IP

One of the functions of the .htaccess file is that it allows you to block certain IPs or IP ranges from visiting your website. To implement this function, you need to add the following commands in the .htaccess file.

Reject Specific IP

Using the following command, you can block any IP address you want. Just add the following command line in your configuration file.

Order Deny,Allow
Deny from 192.168.2.110

//code 192.168.2.110 is specific IPv4 address that being rejected

cmlabs

Reject IP List

You can also reject multiple IP addresses at once, the method is similar to the previous command. However, you need to create a list of IPs that you want to reject as follows:

Order Deny,Allow
Deny from 192.168.2.110
Deny from 192.120.120.120

cmlabs

Blocking Certain Domains

If you want to block a certain domain (eg domain_example.com) from accessing your website, then you can apply the command below. With this setting, websites linking to your web page will display a 403 - forbidden error when someone clicks on your URL via that domain.

SetEnvIfNoCase Referer “domain_example.com” bad_referer
Order Allow,Deny
Allow From ALL
Deny from env=bad_referer

cmlabs

Blocking or Allowing IP Ranges

You can also block or allow certain ranges of IP addresses that try to access your web pages. Notice the line of code that you can add in the following configuration file:

Order Deny, Allow

//command to block IP ranges, asterisk (*) represents all IP octets
Deny from 192.168.*.*

//command to allow IP range
Allow from all


cmlabs

Allow or Deny Access To Certain Folders

The function of .htaccess is to help web owners give authority to anyone who has the right to access folders from a website. For example, setting a difference in access rights owned by visitors, website admins, and others. This aims to maintain the security of important files on the website, the commands that can be used are IndexIgnore */*, and others.

Redirecting a Web Page

When the website is under maintenance, you can set a page redirect via the .htaccess file. In addition, you can also apply redirect practices to increase traffic to the desired web page. The commands that can be used are:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301,NC]

cmlabs

Or you can use the following simpler code:

Redirect 301 /home/html/web/index.html https://www.domainbaru.com/home.html

cmlabs

Creating Custom Error Pages

What's unique about the .htaccess function is that it lets you create custom error pages. This may be necessary under certain conditions, for example when the website is under maintenance or other things. You can simply define your error page in the configuration file.

A number of error messages that may occur are 400 - Bad Request, 401 - Authorization Required, 403 - Forbidden, 404 - Not Found, 500 - Internal Server Error, and others. By creating an error page, you can show the professionalism of the web owner in dealing with website problems. Sample code you can use:

//Redirect error page with HTML file
ErrorDocument 403 /403.html

// Redirect error page with PHP file
ErrorDocument 404 /error.php?q=404

cmlabs

Protecting Website Directory With Password

One of the functions of .htaccess is to support the authentication process on web pages, you can protect your directory by adding a username and password to access it. The code you can add to the .htaccess file is as follows:

AuthName “Your Folder”
AuthUserFile /path/to/password/file/.htpasswd
AuthType Basic
require valid-user

cmlabs

The first line is a command that tells the Apache web server that the website directory you want to password-protect is “Your folders”. The second line shows the directory where you saved the folder.

Then, the next line is the type of authentication you want to use, in the code above the type used is basic HTTP authentication. Whereas the last line is a command saying that you need valid credentials to access the directory.

cmlabs

cmlabs

WDYT, you like my article?

Latest Update
Last updated: Nov 21, 2024
Last updated: Nov 08, 2024
Last updated: Nov 06, 2024
Explore Other Terms

Streamline your analysis with the SEO Tools installed directly in your browser. It's time to become a true SEO expert.

Free on all Chromium-based web browsers

Install it on your browser now? Explore Now cmlabs chrome extension pattern cmlabs chrome extension pattern

Need help?

Tell us your SEO needs, our marketing team will help you find the best solution

Here is the officially recognized list of our team members. Please caution against scam activities and irresponsible individuals who falsely claim affiliation with PT CMLABS INDONESIA DIGITAL (cmlabs). Read more
Marketing Teams

Agita

Marketing

Ask Me
Marketing Teams

Destri

Marketing

Ask Me
Marketing Teams

Thalia

Marketing

Ask Me
Marketing Teams

Irsa

Marketing

Ask Me
Marketing Teams

Yuliana

Business & Partnership

Ask Me
Marketing Teams

Rochman

Product & Dev

Ask Me
Marketing Teams

Said

Career & Internship

Ask Me

Interested in joining cmlabs? Boost your chances of becoming an SEO Specialist with our new program, cmlabs Academy. it's free!

Check

New! cmlabs Added 2 Tools for Chrome Extensions! What Are They?

Check

There is no current notification..