Improving WordPress security with .htaccess

In this article I’ll share some of the security tweaks I tend to add to the .htaccess to improve security of the WordPress-installation.

Hide the wp-config.php file

Since the wp-config.php file contains our database credentials, we do not want this file to be accessible, PERIOD. So by adding this snippet to our .htaccess file we can prevent access to it:

#hide wp-config file

order allow,deny
deny from all

This rule will prevent that the wp-config.php is accessible.

Hide the .htaccess file itself

Preventing abuse by adding rules to our .htaccess is only useful if the .htaccess can’t be compromised itself.

To make sure that doesn’t happen we can add this to the .htaccess file:

#hide htaccess file

order allow,deny
deny from all

Block requests towards the wp-includes folder and files

This is in fact a no-brainer. No legitimate request will ever go towards the wp-includes folder or the files in that folder. On the other hand it’s a popular spot to hide malicious files, such as backdoors and shells.

So let’s make sure this folder and its content isn’t accessible. We can do this by adding this little snippet to our .htaccess:

# Block wp-includes folder and files

RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]

Blocking the TRACE method

Blocking the TRACE method will help to counteract XSS (Cross Site Scripting) attacks.

# Block trace method
RewriteEngine On
RewriteRule .* - [F]

Prevent username enumeration

Within the url structure of WordPress, it’s (in some situations) easy to ascertain the usernames of the authors. This could be done by using following url string:

This will then result in a url similar to this:

You immediately understand this can be used to ascertain a list of usernames, which can then be used for a brute forcing attack.

We can stop this by adding this snippet to our .htaccess file:

#Stop username enumeration
RewriteCond %{REQUEST_URI} !^/wp-admin [NC]
RewriteCond %{QUERY_STRING} author=\d
RewriteRule ^ /? [L,R=301]
By |2022-03-20T11:04:33+00:00February 14, 2022||Comments Off on Improving WordPress security with .htaccess

Share This Story, Choose Your Platform!

Go to Top