Write Review

How to Secure the .htaccess File from Unauthorized Access

Every new WordPress user has some big dreams to build a rock-solid website, but nothing goes what they think. Millions of sites get injected with some malicious code.

There are many essential files in WordPress core, and the .htaccess file is the powerhouse for a WordPress website.

The file contains many rewrite rules, security code, code added by a few plugins, and much more. If you read a WordPress security guide, you merely find something to protect this file.

Most of the people are focusing on changing the default admin username, secure password, disabling directory browsing, and other conventional things.

Let me make you aware that people can have unauthorized access to your .htaccess file and you must stop them.

To accomplish it, you need to understand how to find this file. I hope, you know how to edit the .htaccess file. You can either use an FTP client or cPanel.

Adding the Security Code to the .htaccess File

One of the most significant questions is the best and easiest way to edit this file. If you're a regular WordPress user, you may know that some plugins allow you to edit the robots.txt and .htaccess files.

If you use Yoast SEO, you can easily edit this file. But as long as you have cPanel, you don't need to think about any other alternative.

Not everyone uses Yoast SEO. Let me start the process.

Step 1

Log into your cPanel account and open File Manager
from the Files section.

How to Secure the .htaccess File from Unauthorized Access

I hope you have done the correct settings to show the hidden files. As you know, the .htaccess file is a hidden file have a dot prefix; it means you can get see it if you haven't enabled it to be visible.

Step 2

Depending on your website, you have to search the public_html/root directory for ".htaccess."

If you have a subdomain, you may require to open a new folder to find it. I hope you already know where your website's data is present.

How to Secure the .htaccess File from Unauthorized Access

Right-click to edit the file, click edit.

Step 3

Add this code before #End WordPress.

How to Secure the .htaccess File from Unauthorized Access

<files ~ "^.*.([Hh][Tt][Aa])">
order allow,deny
deny from all

Click on the SaveChanges button showing at the top-right corner.

This code will restrict the unauthorized access to your .htaccess file. Only you can access it because you know the login credentials of your web hosting cPanel account.

How to Secure the .htaccess File from Unauthorized Access

Many WordPress experts have mentioned that an FTP account can be vulnerable, so you should avoid using it.

Nowadays, people have started using SSH, but unfortunately, not every web hosting offers it. Some companies give SSH only with their higher web hosting plans.

You may be wondering about the code, well, it's pretty simple. The code check denies the access from everyone unless they log in via cPanel.

To maintain a secure website, it's essential to add more security layers.

As I have already mentioned, the .htaccess file plays a vital role in handling a WordPress website; you need to save it from getting the malicious code injected.

If you're looking for an alternative to secure this file, you can use any of the security plugins which has such feature.

I have checked All In One Security plugin, which allows you to protect the .htaccess file from unauthorized access within the WordPress admin panel.

I Hope You Can Take the Essential Step to Secure Your Website

Every time a someone finds a malicious code on their website, they fret a lot, which is normal.

But somewhere it's always the user's fault, because they may not have taken the right steps.

Now and then, you may hear about wp-config.php and .htaccess, both the files hold a special place in controlling the website features.

After going through the above tutorial, are you going to use the manual method or a plugin? Depending on your choice, you can choose any, but the code should be in the file.

I would also like to mention, that you can also use the .htaccess file to improve your website's overall security.


Don't you think, you should take every essential step to protect your website from unauthorized access? It's not only about one file, it's about the whole website.

If you allow someone to intrude, they can spread the malicious code to every file and folder of your WordPress website, it's better to take the right step.

Check out these top 3 WordPress hosting services:

Was this article helpful?