.htaccess File Usage Guide
The .htaccess file is a powerful tool that allows you to perform directory-based configuration on the Apache web server.
You can perform the following operations without accessing the main server configuration:
- URL redirects
- SEO compatible URL structures
- Directory encryption
- Custom error pages
- MIME type definition
In this guide, you will learn how to activate, create, and common usage scenarios about the .htaccess file.
What Will You Learn in This Guide?
The following topics will be covered in this document:
- Enabling
.htaccesssupport in Apache - Creating the
.htaccessfile in the correct directory - Using URL redirection and mod_rewrite
- Directory encryption operations
- Add custom error pages and MIME type
- Performance and security implications of
.htaccess
Prerequisites
To follow this guide, you need to have the following components ready:
- VPS with Ubuntu 20.04 or above
- A user with sudo authority
- Active UFW firewall
- Installed Apache Web Server
- Virtual Host configured for the domain
Sample Virtual Host file:
/etc/apache2/sites-available/ornek.com.conf
Enabling and Creating the .htaccess File
Enabling .htaccess in Apache
By default, Apache will not run .htaccess without the AllowOverride setting enabled.
Open the Virtual Host file
sudo nano /etc/apache2/sites-available/ornek.com.conf
Add or edit the following configuration:
<VirtualHost *:80>
ServerName ornek.com
DocumentRoot /var/www/ornek.com
<Directory /var/www/ornek.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
AllowOverride Allsetting enables the use of .htaccess.
Restart Apache service
sudo systemctl restart apache2
Creating the .htaccess File
The .htaccess file should be located in the root directory of your website.
Go to the web root directory:
cd /var/www/ornek.com Create the file:
sudo nano .htaccess
You can now add any Apache directives you want to the .htaccess file.
Common Uses of the .htaccess File
Directory Password Protection
- Creating password file:
sudo htpasswd -c /etc/apache2/.htpasswd genixnode-user
- add in .htaccess
AuthType Basic
AuthName "Korunan Alan"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
This configuration requires username and password authentication to access the directory.
URL Rewrite (mod_rewrite)
You can create clean, SEO-compatible URL structures.
RewriteEngine On
RewriteRule ^sayfa/([0-9]+)$ sayfa.php?id=$1 [NC,L]
This rule makes the following transformation:
site.com/sayfa/5
→
site.com/sayfa.php?id=5
Custom Error Pages
To show custom pages on server errors:
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
Adding MIME Types
To have Apache recognize special file types:
AddType application/x-font-otf .font
Enabling SSI (Server Side Includes)
To use SSI:
AddType text/html .shtml
AddHandler server-parsed .shtml
To run SSI on HTML files:
XBitHack on
Give execute permission to the file:
chmod +x sayfa.html
.htaccess Performance and Security Impacts
Performance
Small delays may occur on large projects because Apache checks the .htaccess file with every request.
Therefore, on high-traffic sites, it is recommended to make settings in the Apache main configuration file.
Security
The .htaccess file allows for quick configuration but can pose security risks if configured incorrectly.
In particular:
File permissions Unauthorized access Misdirections
should be checked carefully.
Frequently Asked Questions (FAQ)
Why is .htaccess a hidden file?
The "." at the beginning Due to its character, it is considered a hidden file in Linux systems.
Can I use another value instead of AllowOverride All?
Yes, but some .htaccess features may be disabled.
Are subdirectories affected by .htaccess settings?
Yes. By default all subdirectories inherit the same rules.
Is Apache configuration or .htaccess more performant?
Apache main configuration is preferred for performance, .htaccess is preferred for flexibility.
Can there be more than one .htaccess in a directory?
Yes. Each directory can contain its own .htaccess file.
Result
In this guide, you learned how to enable the .htaccess file on Apache and apply basic configurations.
.htaccess when used correctly:
Makes URL redirects easier Controls directory access Creates SEO friendly URL structures Lets you customize error pages
To make your infrastructure more powerful, you can easily manage your Apache configurations by using high-performance servers on GenixNode.

