📑 Daftar Isi
- What Is .htaccess?
- The History of .htaccess
- How to Use .htaccess — Step by Step
- 1. Creating a .htaccess File
- 2. Most Common .htaccess Directives
- 3. Redirect HTTP to HTTPS
- 4. Password Protect a Directory
- 5. Block Specific IP Addresses
- 6. Custom Error Pages
- 7. Cache Control via .htaccess
- 8. Prevent Image Hotlinking
- Does .htaccess Work on All Web Servers?
- Apache — Full Support
- LiteSpeed — Mostly Supported
- OpenLiteSpeed — Limited Support
- Nginx — NOT Supported
- IIS Windows — No Native Support
- Caddy and Lighttpd — Not Supported
- Complete .htaccess Compatibility Table
- .htaccess Troubleshooting — Common Errors
- .htaccess FAQ
So here’s the deal — I still remember my first day at a web hosting company. Fresh NOC engineer, got production server access, and my first task was to fix a client’s 403 error. I opened the file manager and saw this weird file called .htaccess. Hidden file, no extension, filled with cryptic syntax. Panic mode? Absolutely. They never taught us about .htaccess in college. And that was the turning point — I dove deep into learning about this tiny file that packs a massive punch.
If you’re just starting out in web hosting or sysadmin work, you’ll eventually run into .htaccess. And honestly? It can be confusing as hell at first. Don’t worry — in this guide, I’ll walk you through everything: the history of .htaccess, detailed usage examples, and the big question — does it work on all web servers? Let’s dive in.
What Is .htaccess?
Simply put, .htaccess is a per-directory configuration file used by Apache web server. The name stands for “hypertext access”. It’s a hidden file — hence the dot prefix — which means you won’t see it in FTP clients or file managers unless you enable “show hidden files”.
Its main job is to tell Apache how to handle requests in a specific directory. Want to redirect HTTP to HTTPS? Done. Create custom error pages? Easy. Block access to certain folders? No problem. Control browser caching? You got it. All of this can be done through .htaccess without restarting Apache. This is especially useful in shared hosting environments where you don’t have access to the main Apache config file.

The tricky part is that many beginners find .htaccess intimidating. They’re scared to edit it because one wrong character can bring down an entire website. But once you understand the basics, .htaccess becomes one of the most powerful tools in your sysadmin toolkit. What makes it even more confusing is that not all web servers support .htaccess — so this knowledge is specific to Apache and its derivatives. Understanding these limitations before you start experimenting is absolutely critical.
The History of .htaccess
Let’s go back in time a bit. .htaccess was first introduced in Apache HTTP Server version 1.x, around the mid-1990s. Apache itself was released in 1995 as a fork of the NCSA HTTPd server. The .htaccess file was born out of a practical need — how do you give configuration power to users without giving them root access to the server?
Here’s the scenario — back in the early days of web hosting, most providers ran shared hosting environments. One Apache server hosting hundreds of websites. The challenge was: how do you let users manage their own redirects, password protections, and custom error pages without giving them full server access? The answer was .htaccess. Users could write their own configuration rules in a file placed inside their website directory. Apache would read this file on every request. It was a brilliant decentralized approach for its time — and it’s still widely used today.
Over the years, .htaccess evolved significantly. Apache 2.x added support for more directives and features. Now in Apache 2.4, .htaccess supports almost every directive available in the main configuration, with only a few exceptions that can’t be overridden at the directory level. Modern frameworks like WordPress, Joomla, and Drupal still rely heavily on .htaccess for pretty permalinks and redirects.
How to Use .htaccess — Step by Step
1. Creating a .htaccess File
Creating the file is straightforward. Just create a new file named “.htaccess” with the leading dot and no extension. Easiest way using the command line:
nano /home/user/public_html/.htaccess
Or via SSH:
echo "# Start .htaccess configuration" > .htaccess
If you’re using cPanel, you can create it through File Manager — click Create New File, name it “.htaccess”, and make sure Show Hidden Files is enabled.
2. Most Common .htaccess Directives
Here’s a handy reference table of the directives you’ll use most often:
| Directive | Function | Example Usage |
|---|---|---|
| RewriteEngine On | Enable URL rewriting | RewriteEngine On |
| Redirect | Redirect one URL to another | Redirect 301 /old.html /new.html |
| ErrorDocument | Custom error pages | ErrorDocument 404 /404.html |
| Require | Allow or deny access | Require not ip 192.168.1.1 |
| Options | Control directory features | Options -Indexes |
| AddType | Add MIME types | AddType application/x-httpd-php .html |
| SetEnv | Set environment variables | SetEnv APPLICATION_ENV production |
3. Redirect HTTP to HTTPS
This is the most common use case. Simple mod_rewrite solution:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Breakdown: line 1 turns on the rewrite engine, line 2 checks if HTTPS is off, line 3 redirects to HTTPS with a 301 permanent status. Works 99% of the time.
4. Password Protect a Directory
Use .htaccess together with .htpasswd. In your .htaccess file:
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/user/public_html/.htpasswd
Require valid-user
Then create the .htpasswd file:
htpasswd -c /home/user/public_html/.htpasswd admin
Anyone trying to access that directory will get a login prompt. Perfect for admin panels, staging sites, or internal tools.
5. Block Specific IP Addresses
Need to block traffic from certain IPs? In Apache 2.4+, use the Require directive:
Require all granted
Require not ip 192.168.1.100
Require not ip 10.0.0.0/24
This is extremely useful when you’re dealing with brute force attacks from specific IP ranges.
6. Custom Error Pages
Make your error pages more helpful and brand-consistent:
ErrorDocument 400 /error/bad-request.html
ErrorDocument 401 /error/unauthorized.html
ErrorDocument 403 /error/forbidden.html
ErrorDocument 404 /error/not-found.html
ErrorDocument 500 /error/server-error.html
7. Cache Control via .htaccess
Speed up your site with browser caching:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>
8. Prevent Image Hotlinking
Stop other websites from stealing your bandwidth:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://yourdomain.com/.*$ [NC]
RewriteRule .(jpg|jpeg|png|gif)$ - [F]
Does .htaccess Work on All Web Servers?
This is the million-dollar question. Short answer: NO. Not all web servers support .htaccess. Let’s break it down server by server.
Apache — Full Support
Apache is the native home of .htaccess. It’s 100% supported as long as AllowOverride is enabled in the Apache configuration. You can use all the directives we covered above.
LiteSpeed — Mostly Supported
LiteSpeed Enterprise was designed to be Apache-compatible, including .htaccess. Most directives work out of the box. However, some Apache-specific directives may not work — always check LiteSpeed’s official docs before migrating.
OpenLiteSpeed — Limited Support
The free version also supports .htaccess but with more limitations. Rewrite rules generally work, but directives like Options may not be fully supported. Recommended for testing, not large-scale production.
Nginx — NOT Supported
This is the big one. Nginx does NOT support .htaccess at all. Why? Because Nginx takes a different approach — all configuration goes into nginx.conf and requires a reload for changes. This makes Nginx faster since it doesn’t scan directories for .htaccess files. If you’re migrating from Apache to Nginx, use online converters like winginx.com or the htaccess2nginx tool.
IIS Windows — No Native Support
IIS has its own web.config XML-based system. Third-party extensions like Helicon Ape or IIS URL Rewrite Module can interpret some .htaccess rules, but compatibility isn’t guaranteed.
Caddy and Lighttpd — Not Supported
Caddy uses its own Caddyfile, and Lighttpd uses lighttpd.conf. Both take a centralized configuration approach like Nginx.
Complete .htaccess Compatibility Table
| Web Server | .htaccess Support | Important Notes |
|---|---|---|
| Apache | 100% | Native support, depends on AllowOverride |
| LiteSpeed Enterprise | 90-95% | Most directives work |
| OpenLiteSpeed | 70-80% | Rewrite works, some limitations |
| Nginx | NO | Requires manual conversion |
| IIS | No native | Use Helicon Ape or IIS Rewrite Module |
| Caddy | NO | Uses Caddyfile |
| Lighttpd | NO | Config via lighttpd.conf |
.htaccess Troubleshooting — Common Errors
| Error | Cause | Quick Solution |
|---|---|---|
| 500 Internal Server Error | Syntax error in .htaccess | Check Apache error log for exact line |
| 403 Forbidden | Deny rule or wrong permissions | Check Options -Indexes and Require directive |
| Redirect Loop | RewriteCond mismatch | Add proper conditions to prevent loop |
| 500 on Upload | BOM format from Notepad | Save as UTF-8 without BOM |
The 500 error is the most common one. Don’t panic — just check /var/log/apache2/error.log, look for lines mentioning .htaccess, and the syntax error will be described in detail. Pro tip from experience: always backup your .htaccess before editing. Just save it as .htaccess.bak. If something breaks, rename it back and you’re good.
Want to learn more? Check out these related guides:
- How to Setup Redirect in cPanel
- Troubleshoot Error 500 Internal Server
- Website Speed Optimization with Caching
.htaccess FAQ
Q: What’s the difference between .htaccess and httpd.conf?
.htaccess is a per-directory config file, while httpd.conf is Apache’s main configuration. .htaccess can be edited without restarting Apache and is ideal for shared hosting. httpd.conf requires a restart and is only accessible by server admins.
Q: Why isn’t my .htaccess file working?
Several possibilities: (1) AllowOverride isn’t enabled, (2) The file isn’t named exactly “.htaccess”, (3) The file has a BOM causing errors, (4) Syntax error. Check Apache error logs for details.
Q: Does .htaccess affect server performance?
Yes, it does. Apache scans directories and reads .htaccess on every request. On high-traffic servers, best practice is to move rules into the main Apache config and disable AllowOverride.
Q: Can I use .htaccess with WordPress?
Absolutely! WordPress automatically writes rewrite rules to .htaccess for pretty permalinks. Plugins like W3 Total Cache also add caching rules there. But be careful — too many plugins can bloat your .htaccess and slow things down.
Q: How do I backup .htaccess before editing?
Simple — just copy it: cp .htaccess .htaccess.bak. If something breaks, restore with mv .htaccess.bak .htaccess. Always backup before editing, especially on production servers!
Alright, that’s a wrap on this .htaccess guide. Hopefully now you have a much better understanding of what .htaccess is, where it came from, how to use it, and which servers actually support it.
Go ahead and try out the steps above on your server. Start with something simple like redirecting HTTP to HTTPS or setting up a custom 404 page. If you run into issues, check the error logs and compare with the troubleshooting table I shared.
Got a .htaccess war story or a unique problem you solved? Drop it in the comments — you might help someone else dealing with the same issue. And if you’ve got a more efficient way, I’m always open to learning new tricks. Good luck and happy configuring!