Wednesday 19 October 2016

How To Protect WordPress Website From Hackers | 12 Most Useful .htaccess Tricks for WordPress To Protect From Hackers

Best WordPress Tutorials

 12 Most Useful .htaccess Tricks for WordPress To Protect From Hackers

 How To Protect WordPress Website From Hackers 

12 Most Useful .htaccess Tricks for WordPress Ever new User Must Do To Protect From Hackers

WordPress tutorials allow you to extend the power of WordPress. Our step by step WordPress tutorials are easy to understand and follows the WordPress best practices. Don't waste your time buying books that cover just the basics. These WordPress tutorials contain real-life examples, tips, and hacks that allows you to learn WordPress faster.

12 Most Useful .htaccess Tricks for WordPress

What is .htaccess File and How to Edit it?

The .htaccess file is a server configuration file. It allows you to define rules for your server to follow for your website.
WordPress uses .htaccess file to generate SEO friendly URL structure. However, this file can do a lot more.
The .htaccess file is located in your WordPress site’s root folder. You will need to connect to your website using an FTP client to edit it.

Before editing your .htaccess file, it is important to download a copy of it to your computer as backup. You can use that file in case anything goes wrong.
Having said that, let’s take a look at some useful .htaccess tricks for WordPress that you can try.

1. Protect Your WordPress Admin Area

You can use .htaccess to protect your WordPress admin area by limiting the access to selected IP addresses only. Simply copy and paste this code into your .htaccess file:
view source
print?
01AuthUserFile /dev/null
02AuthGroupFile /dev/null
03AuthName "WordPress Admin Access Control"
04AuthType Basic
05<LIMIT GET>
06order deny,allow
07deny from all
08# whitelist Syed's IP address
09allow from xx.xx.xx.xxx
10# whitelist David's IP address
11allow from xx.xx.xx.xxx
12</LIMIT>
Don’t forget to replace xx values with your own IP address. If you use more than one IP address to access the internet, then make sure you add them as well.
For detailed instructions, see our guide on how to limit access to WordPress admin using .htaccess.

2. Password Protect WordPress Admin Folder

Password Protect WordPress Admin Folder
Password Protect WordPress Admin Folder


If you access your WordPress site from multiple locations including public internet spots, then limiting access to specific IP addresses may not work for you.
You can use .htaccess file to add an additional password protection to your WordPress admin area.
First, you need to generate a .htpasswds file. You can easily create one by using this online generator.
Upload this .htpasswds file outside your publicly accessible web directory or /public_html/ folder. A good path would be:
/home/user/.htpasswds/public_html/wp-admin/passwd/
Next, create a .htaccess file and upload it in /wp-admin/ directory and then add the following codes in there:
view source
print?
01AuthName "Admins Only"
02AuthUserFile /home/yourdirectory/.htpasswds/public_html/wp-admin/passwd
03AuthGroupFile /dev/null
04AuthType basic
05require user putyourusernamehere
06<Files admin-ajax.php>
07Order allow,deny
08Allow from all
09Satisfy any
10</Files>
Important: Don’t forget to replace AuthUserFile path with the file path of your .htpasswds file and add your own username.
For detailed instructions, see our guide on how to password protect WordPress admin folder.

3. Disable Directory Browsing
Disable Directory Browsing in wordprss
Disable Directory Browsing in wordprss


Many WordPress security experts recommend disabling directory browsing. With directory browsing enabled, hackers can look into your site’s directory and file structure to find a vulnerable file.
To disable directory browsing on your website, you need to add the following line to your .htaccess file.
view source
print?
1Options -Indexes
For more on this topic, see our guide on how to disable directory browsing in WordPress.

4. Disable PHP Execution in Some WordPress Directories

Sometimes hackers break into a WordPress site and install a backdoor. These backdoor files are often disguised as core WordPress files and are placed in /wp-includes/ or /wp-content/uploads/ folders.
An easier way to improve your WordPress security is by disabling PHP execution for some WordPress directories.
You will need to create a blank .htaccess file on your computer and then paste the following code inside it.
view source
print?
1<Files *.php>
2deny from all
3</Files>
Save the file and then upload it to your /wp-content/uploads/ and /wp-includes/ directories. For more information check out our tutorial on how to disable PHP execution in certain WordPress directories.

5. Protect Your WordPress Configuration wp-config.php File

Probably the most important file in your WordPress website’s root directory is wp-config.php file. It contains information about your WordPress database and how to connect to it.
To protect your wp-config.php file from unathorized access, simply add this code to your .htaccess file:
view source
print?
1<files wp-config.php>
2order allow,deny
3deny from all
4</files>

6. Setting up 301 Redirects Through .htaccess File

Using 301 redirects is the most SEO friendly way to tell your users that a content has moved to a new location. If you want to properly manage your 301 redirects on posts per post basis, then check out our guide on how to setup redirects in WordPress.
On the other hand, if you want to quickly setup redirects, then all you need to do is paste this code in your .htaccess file.
view source
print?
1Redirect 301 /oldurl/ http://www.example.com/newurl
2Redirect 301 /category/television/ http://www.example.com/category/tv/

7. Ban Suspicious IP Addresses

Are you seeing unusually high requests to your website from a specific IP address? You can easily block those requests by blocking the IP address in your .htaccess file.
Add the following code to your .htaccess file:
view source
print?
1<Limit GET POST>
2order allow,deny
3deny from xxx.xxx.xx.x
4allow from all
5</Limit>
Don’t forget to replace xx with the IP address you want to block.

8. Disable Image Hotlinking in WordPress Using .htaccess

Other websites directly hotlinking images from your site can make your WordPress site slow and exceed your bandwidth limit. This isn’t a big issue for most smaller websites. However, if you run a popular website or a website with lots of photos, then this could become a serious concern.
You can prevent image hotlinking by adding this code to your .htaccess file:
view source
print?
1#disable hotlinking of images with forbidden or custom image option
2RewriteEngine on
3RewriteCond %{HTTP_REFERER} !^$
4RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?wpbeginner.com [NC]
5RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
6RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
This code only allows images to be displayed if the request is originating from wpbeginner.com or Google.com. Don’t forget to replace wpbeginner.com with your own domain name.
For more ways to protect your images see our guide on ways to prevent image theft in WordPress.

9. Protect .htaccess From Unauthorized Access

As you have seen that there are so many things that can be done using the .htaccess file. Due to the power and control it has on your web server, it is important to protect it from unauthorized access by hackers. Simply add following code to your .htaccess file:
view source
print?
1<files ~ "^.*\.([Hh][Tt][Aa])">
2order allow,deny
3deny from all
4satisfy all
5</files>

10. Increase File Upload Size in WordPress

There are different ways to increase the file upload size limit in WordPress. However, for users on shared hosting some of these methods do not work.
One of the methods that has worked for many users is by adding following code to their .htaccess file:
view source
print?
1php_value upload_max_filesize 64M
2php_value post_max_size 64M
3php_value max_execution_time 300
4php_value max_input_time 300
This code simply tells your web server to use these values to increase file upload size as well as maximum execution time in WordPress.

11. Disable Access to XML-RPC File Using .htaccess

Each WordPress install comes with a file called xmlrpc.php. This file allows third-party apps to connect to your WordPress site. Most WordPress security experts advise that if you are not using any third party apps, then you should disable this feature.
There are multiple ways to do that, one of them is by adding the following code to your .htaccess file:
view source
print?
1# Block WordPress xmlrpc.php requests
2<Files xmlrpc.php>
3order deny,allow
4deny from all
5</Files>
For more information, see our guide on how to disable XML-RPC in WordPress.

12. Blocking Author Scans in WordPress

A common technique used in brute force attacks is to run author scans on a WordPress site and then attempt to crack passwords for those usernames.
You can block such scans by adding the following code to your .htaccess file:
view source
print?
1# BEGIN block author scans
2RewriteEngine On
3RewriteBase /
4RewriteCond %{QUERY_STRING} (author=\d+) [NC]
5RewriteRule .* - [F]
6# END block author scans
For more information, see our article on how to discourage brute force attacks by blocking author scans in WordPress.
We hope this article helped you learn the most useful .htaccess tricks for WordPress. You may also want to see our ultimate step by step WordPress security guide for beginners.

No comments:

Post a Comment