New Service Alert! Let us be part of your journey to success. SEO for Small Business is Here.

Where might you have seen our work?

Small places create combinations, but crosses that occur cannot provide many combinations. So be careful in making justifications, especially SEO.

.htaccess

Last updated: Jul 26, 2022

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

Note: We have attached some of the most common questions asked by users below, along with their answers. To use the cmlabs Keyword Ranking Tracker application, you don't need to request for a quote from marketing. Please click login to the application.

WDYT, you like my article?

Need help?

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

Marketing Teams

Laras

Marketing

Ask Me
Marketing Teams

Vanessa

Marketing

Ask Me

As an alternative, you can schedule a conference call with our team

Schedule a Meeting?