This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

How to Protect WordPress with .htaccess

Updated on  29th March

5 Min Read
protect-wordpress with htaccess
Reading Time: 5 minutes

WordPress – The most widely used Content Management System (CMS) in today’s internet realm – due to its popularity, is an incredibly attractive target for hackers. .htaccess is the most essential file for any website. You can do a lot of things using .htaccess file. You can restrict your website access. It can be placed in any folder of the website to change the behavior of that folder.

Today, I’ll explain a bit about securing WordPress website using .htaccess file.


As you’re aware, .htaccess is the control room of your whole website. Even a single misspelled dot (.) can destroy your complete website. Before making any changes, back up your .htaccess file to an offsite location.

So, here we go!

By default, every web hosting has .htaccess file in the root directory. If it is not available, you can create one by opening Notepad and saving it with the name “.htaccess”. Be sure it’s not “htaccess”, its “.htaccess” and set “Save as type” to “All files”. Upload it to the root directory of your WordPress hosting.

Save as .htaccess

1. Protect .htaccess

.htaccess has the ability to control your whole website. It is important to first protect this file from unauthorized users. By using the snippet below, you can restrict access to unauthorized users. But, you can edit the file from FTP and your hosting.

Just copy and paste the snippet below into your .htaccess file.

2. Protect wp-config.php

In WordPress, wp-config.php is the file where your hosting, database and other important credentials are saved. Therefore it is also required to restrict unauthorized access to this file. Copy and paste lines of code below, in your .htaccess file.

3. Protect /wp-contents/

Wp-contents is the folder in your WordPress directory that contains files of your themes, plugins, media and cached files. That’s why this directory is the main target for hackers and spammers. Create a separate .htaccess file. Copy and paste the snippet below, in that file.

Now, upload it to “” folder.

By uploading this file, only media files are allowed to be uploaded including XML, CSS, JPG, JPEG, PNG, Gif, and Javascript. All other file types will be denied.

4. Protect Include-Only files

There are some areas in WordPress that never have to be accessed by the user. It’s better to block access to those files. You can place blocks by adding the snippet below, into your .htaccess file.

5. Limit Access to WordPress Admin Panel

What if someone gets access to your Admin Panel? This is another entry point for hackers to destroy your website. If anyone gains access to the admin panel, he can do almost anything to your website. To get rid of such attempts, you can provide admin access to specific IP’s only. Create another .htaccess file by pasting the snippet below and upload it to “” folder.

If someone else tries to login to your website – other than the above IPs – he will face the error shown below.

Internal Server Error

Don’t forget to change “” to your own IP address. You can easily get your IP by visiting “What is my ip”. If you’ve got more than one moderator, you can also add more IP’s like this.

6. BAN Someone from Your Website

If you know someone is abusing your website, continuously spamming or launching hacking attempts, you can get his IP from your WordPress Admin Panel and add that IP to your .htaccess file to completely restrict access to your website. Just copy & paste the snippet below into your .htaccess file and he’ll not be able to access your website. Be sure to replace IP address of that spammer.

He’ll get this error whenever he wants to access your website.

Forbidden Error

7. Disable Directory Browsing

Unauthorized access to your files and folders can be a major security risk.

By adding the snippet below into your .htaccess file, access to your directories will be disabled.

8. Redirect a URL

A 301 error tells search engines that a URL has been permanently moved to another new location. You can redirect that old URL to the new one by using .htaccess. This is not limited to URLs only, you can redirect a folder, page or even a complete website. You just have to add the few lines of code below, to your .htaccess file.

The first step to secure your WordPress website is your hosting. Sign up on secure Managed WordPress Cloud hosting. Then, you’ve got to secure it manually by implementing the above mentioned tricks.

To move to the next step for complete prevention from different kinds of attacks, there are a number of Plugins available on the WordPress repository. Some of the most popular ones are:

  1. Wordfence
  2. iThemes Security
  3. Sucuri Security
  4. All In One WP Security & Firewall

That about covers it for now. I hope you found this post informative. Implement these tricks on your WordPress site, and then test your security or you can follow these 10 WordPress security issues & how to fix them. Let me know if you have any questions or concerns, and I’ll get back to you.

Share your opinion in the comment section. COMMENT NOW

Mustaasam Saleem

Mustaasam is the WordPress Community Manager at Cloudways - A Managed WordPress Hosting Platform, where he actively works and loves sharing his knowledge with the WordPress Community. When he is not working, you can find him playing squash with his friends, or defending in Football, and listening to music. You can email him at

Make Your WordPress Website 100% Faster.

Host it now on Cloudways WordPress Hosting Platform.

Get Our Newsletter
Be the first to get the latest updates and tutorials.