Sometimes the web server configuration needs to be changed so that the website can run smoothly. One way is to use the httpd.conf file. This file, httpd.conf, is the Apache Web Server configuration file. Any Apache configuration can be changed using this file. Unfortunately not all users can access it. Especially if you use shared hosting.
Although access to httpd.conf is limited, there is another way to modify the Apache Web Server configuration, namely using the .htaccess file. The .htaccess file allows you to implement several instructions to override the httpd.conf configuration without requiring access to the httpd.conf file directly.
There are many configurations that can be done using .htaccess. But some configurations sometimes make users confused. To make it easier to understand .htaccess, this article will discuss what .htaccess is and how to use it.
What is .htaccess?
Before learning htaccess further, let’s get to know the .htaccess file first. htaccess is a configuration file that runs on websites to manage some of the Apache Web Server configurations. The .htaccess file can be used to change configurations such as activating and deactivating running functionality, redirecting, rearranging URLs, protecting directories using a password, hotlink protection, blocking certain IPs, changing time zones, redirecting, and many others.
You can find this .htaccess file on various websites. For example a website built with WordPress or CodeIgniter’s default .htaccess file. Unfortunately, this file is sometimes hidden in the website directory. You need to activate the “show hidden files” feature to be able to find them.
How To Use .htaccess
You must have login access to the website directory in order to modify the .htaccess file. There are two ways that are usually used, namely using the File Manager and access using FTP.
1. Access Using File Manager
In order to use this method, the condition is that you must have a username and password to access cPanel.
After getting access to enter cPanel, please follow the hidden file access guide in File Manager to access the .htaccess file on hosting using a file manager.
2. Access Using FTP
Apart from using access to cPanel, there is another way to access the .htaccess file, namely using an FTP client. You must have a username and password.
You can use various FTP client applications available on the internet such as FileZilla, WinSCP, CyberDuck, and SmartFTP. But most use FileZilla as their FTP application.
If you use FileZilla, you can use the previously reviewed guide to using FileZilla.
What if I can’t find the .htaccess file even though the “show hidden files” feature has been activated? It is possible that the Apache Web Server does not yet have an .htaccess file or the .htaccess file is missing. So in order to use the .htaccess feature, you just need to create it.
Example Usage .htaccess
All settings in .htaccess can be set using httpd.conf, but not all settings in httpd.conf can be set using .htaccess.
While limited, there’s a lot you can do with .htaccess. Below are some examples of using .htaccess to configure the Apache Web Server configuration for your website.
1. Use of Comments
The .htaccess file is usually created by default in the folder. In the file usually there are several lines of configuration available. If you want to add a completely new configuration. You can comment using the “#” sign on each existing line of code. Then insert the new script underneath.
Every line marked with a hash symbol (#) means it is inactive or becomes a commented line which means when the .htaccess file is read, every line of code marked with a hash symbol is not read.
2. Full .htaccess Overriding
You can set the level of access rights that can be done using .htaccess. When you set full .htaccess overriding, then all configuration in .htaccess will be applied like httpd.conf.
Full .htaccess overriding can be done by adding the script below:
# Allow .htaccess files their full powerAllowOverride All
3. Limited Overriding
In addition to overriding functions, you can restrict .htaccess privileges to the system. Below is an example of a .htaccess script that can only ovveride authorization and indexes.
# Only allow .htaccess files to override Authorization and IndexesAllowOverride AuthConfig Indexes
4. Check Status of .htaccess
It is possible that the hosting service provider does not allow users to access the httpd.conf file. So how do you know if .htaccess is active or not? One way is to check the status of the .htaccess itself using the “is_htaccess_enabled” feature.
In web hosting, when you access a website, the Apache Web Server will respond by directing requests from the browser to a specific folder on the hosting. Generally the first file accessed is .htaccess.
When Apache accesses the .htaccess file, it tells Apache which file to load first.
So to make sure .htaccess is running or not, the way to check is quite simple. You simply create an is_active.html file and fill it with some script code or just text. Then point the .htaccess file to access the is_active.html file before accessing index.html.
If .htaccess support is not enabled, Apache by default ignores the is_active.html file and then opens the index.html file anyway. However, if the .htaccess file is active, the browser will access the is_active.html file and display the is_active.html page which has included a few lines of code or text earlier.
5. Redirects
Htaccess Redirect allows you to redirect visitors from one part of the website to another. This command is quite useful when you are moving old content and want to redirect visitors from the old content to the location of the new content.
You can redirect using the command below:
Redirect /old_dir/ https://www.yourdomain.com/new_dir/index.html
The above command will provide information to the Apache Web Server with logic like this:
“If a visitor requests a document located in the ‘/old_dir/’ directory, the system will display the document located in ‘/new_dir/index.html'” .
6. Password protection
Apache Web Server offers password protection and authentication features. You can password protect a folder by requiring a password to access it.
Folders protected by .htaccess will show a login pop-up when you try to access the protected folder.
If you want to enable this feature, the first thing you need to do is specify which directories you want to protect. Then directly enter the script below into the .htaccess file. Create an .htaccess file if you don’t already have one.
AuthName “Member’s Area Name”AuthUserFile /path/to/password/file/.htpasswdAuthType Basicrequire valid-user
7. Block Visitor Based on IP
How to limit website access htaccess by Apache allows you to deny access from visitors coming from certain IPs. This feature is very useful for blocking unwanted visitors, or only allowing certain IPs to access certain areas, such as administration areas and such.
Below are examples of settings used to restrict or block visitors using certain IP addresses.
order allow, deny from 255.0.0.0 deny from 192.168.1.allow from all
The above line of code directs the Apache Web Server to block visitors from the IPs ‘255.0.0.0’ and ‘192.168.1.’. The second IP (‘192.168.1.’) does not use the 4th digit, which means that all IPs that use the first three digits 192.168.1 will be blocked.
8. Block Visitors Based on Origin URL
In addition to the blocking feature based on IP address offered by Apache Web Server, you can also use the method of blocking websites based on htaccess countries. This setting is useful when you realize that a website is getting access from a website that is indicated to be spamming or trying to hack your website.
Use of this feature requires you to enable ‘mod_rewrite’ on the server. So you need to make sure through the system administrator or hosting service provider that the ‘mod_rewrite’ feature is active.
Below is an example of a script that is used to block access from certain domains or URLs (referrers):
RewriteEngine on# Options +FollowSymlinksRewriteCond %{HTTP_REFERER}otherdomain\.com [NC]RewriteRule .* – [F]
The line of code above provides information to the Apache Web Server to block traffic from the URL domainlain.com. The text ‘[NC]’ after the referrer domain provides information that the referrer domain is not case sensitive.
At this point, you already know what a .htaccess file is and some examples of its use. Of course, this file is not without problems. One of them is the .htaccess error in WordPress, which we have made a guide to in the article on how to fix the WordPress htaccess file.
Closing
The .htaccess file makes it easy for you to reset the web server. The existence of this file makes you do not need access to httpd.conf which sometimes access is limited by the hosting service provider. While not all httpd.conf settings work in .htaccess, there is a lot of httpd.conf functionality that you can change in .htaccess.
We hope this guide will help you to understand how to use .htaccess properly. Maximum use of .htaccess can of course make the website run optimally.
If you liked this article, don’t forget to subscribe to get the latest information from us, or if you still have questions, please leave a comment in the column below.
[hurrytimer id=”247″]
