Security

Simple .htaccess Security Rules to Protect Your Site

Want to harden your website? Learn how to use simple .htaccess security rules to block bad bots, protect core files, and prevent common attacks. Free snippets inside.

Maya OkaforBy Maya Okafor·September 22, 2026·How we test

What is .htaccess and Why Use It for Security?

Your website's `.htaccess` file is a powerful configuration file used on web servers running Apache software. It lets you override many server settings on a per-directory basis. For security, this means you can create specific rules that act as a first line of defense, intercepting malicious requests before they even get a chance to run any of your site's code.

Think of it as a bouncer at the door of your website. While a full security system (like a WordPress plugin) might be monitoring things inside, the `.htaccess` file can stop known troublemakers from even getting in. This approach is extremely efficient, as it’s handled directly by the server, reducing load on your application and improving your website’s performance under attack.

Using `.htaccess` for security isn’t a replacement for comprehensive security practices like strong passwords and regular updates, but it is an essential, highly effective layer. It's a fast, low-overhead way to implement fundamental security protections that every website should have in place from day one.

Rule 1: Disable Public Directory Browsing

By default, if a visitor navigates to a folder on your server that doesn't contain an index file (like index.html or index.php), the server might display a list of all files and folders within that directory. This is a security risk, as it gives away your site structure and can expose sensitive files, plugin versions, or temporary backup files you forgot about.

Fortunately, blocking this is incredibly simple. Just add one line to the top of your `.htaccess` file to disable this feature across your entire site. This simple command tells Apache not to generate directory listings, showing a '403 Forbidden' error instead. It's a basic but fundamental step that closes an unnecessary window for potential attackers.

``` # Disable directory browsing Options -Indexes ```

Rule 2: Protect Your Most Sensitive Files

Your website has critical files that should never be accessible from a web browser. The most famous example is `wp-config.php` on WordPress sites, which contains your database credentials. Other files like `.htaccess` itself, `error_log`, and `php.ini` should also be locked down tightly, as they can reveal server configurations and vulnerabilities.

You can use a simple `<Files>` directive to block web access to these specific files. This code tells Apache to deny any direct request for them. You can customize this block by adding more filenames, each separated by a pipe character. This is a targeted and highly effective way to shield your configuration from prying eyes.

``` # Protect sensitive files <FilesMatch "^(wp-config\.php|\.htaccess|error_log|php.ini)"> Order allow,deny Deny from all </FilesMatch> ```

Rule 3: Block Malicious IP Addresses & Bad Bots

If you've ever checked your server logs, you've likely seen repeated, suspicious requests from the same IP addresses. These could be brute-force login attempts, comment spam bots, or vulnerability scanners. Blocking these known-bad actors at the server level is an efficient way to stop their activity completely.

The 'Order, deny, allow' directive is your tool for this job. You can add as many 'Deny from' lines as you need to build a blocklist. While this can become a game of whack-a-mole, it's extremely useful for stopping a persistent, active attack from a specific source without needing a plugin. You can typically find these IPs in your cPanel's 'Raw Access Logs' or 'AWStats' reports.

``` # Block malicious IP addresses Order allow,deny Allow from all Deny from 123.45.67.89 Deny from 98.76.54.32 ```

Before You Edit: Common Mistakes & Best Practices

The single most important rule is to **always back up your current .htaccess file** before making any changes. A single syntax error or typo can cause a '500 Internal Server Error' and take your entire website offline. Simply download a copy to your local computer before you begin editing.

Add one security rule at a time, saving the file and testing your website after each addition. This includes checking your homepage, a sub-page, and your admin login area. If the site breaks, you’ll know exactly which rule caused the issue. This methodical approach prevents a lot of troubleshooting headaches.

Remember that `.htaccess` is an Apache-specific feature. If your web host uses Nginx (a common high-performance alternative), these rules will not work. Nginx users must implement similar security logic within their `nginx.conf` file, which uses a different syntax and structure. Always confirm your server type with your hosting provider if you are unsure.

Frequently asked

Questions readers ask about this topic

Will editing .htaccess break my website?

Yes, it can. A syntax error can cause a 500 Internal Server Error, making your site inaccessible. Always back up the file before editing and add rules one by one, testing your site after each change.

Where is the .htaccess file located?

The .htaccess file is usually located in your website's root directory, often named `public_html` or `www`. You may need to enable 'Show Hidden Files' in your file manager or FTP client to see it, as files starting with a dot are hidden by default.

Is using .htaccess better than a security plugin?

It's a different but complementary tool. .htaccess rules are faster as they're handled by the server before your application (like WordPress) even loads. Security plugins offer user-friendly interfaces and more complex features like malware scanning. Using both provides layered, robust protection.

Do these .htaccess rules work on Nginx servers?

No, .htaccess files are only for Apache web servers. Nginx, a popular alternative, does not use .htaccess and requires that you add similar security rules to its own `nginx.conf` configuration file using Nginx's specific syntax.
Keep exploring · Security

Where to go next on Hostilo

Newsletter

One email a month. Hosting deals, new reviews, no fluff.

Related reading