htaccess tips and tricks to prevent your wordpress site from hackers

A .htaccess (hypertext access) file is the common name of a directory-level configuration file which allows decentralized management of web server configuration.  A .htaccess file is always added to the root directory, it can override many other configuration settings which includes server’s global configuration, content type and character set.
Using these htaccess hacks have proven to be useful for our blog from spammers and third party automated software trying to enter our blog. These hacks not only prevents your website from hackers but also improve speed and functionality of your blog/website.

1. Redirect www to non www or vice versa

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

2. Allow only your IP adress on the wp-admin directory

Replace your IP with allow from xx.xx.xx.xx which will only allow your IP to access wp-admin directory.

AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "Wordpress Admin Access Control"
AuthType Basic
order deny,allow
deny from all
allow from xx.xx.xx.xx

3. Deny access to your wp-config.php file

# protect wpconfig.php
<files wp-config.php>
order allow,deny
deny from all

4. Limit the File upload size to 10MB

# limit file uploads to 10mb
LimitRequestBody 10240000

5. Blacklist undesired users and bots ip address

order allow,deny
allow from all
deny from 123.456.789
deny from 93.121.788
deny from 223.956.789
deny from 128.456.780

6. Redirect visitors to a maintenance page

RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^
RewriteRule $ /maintenance.html [R=302,L]

7. Force Caching with htaccess

The following htaccess code won’t help the initial pageload, but it will significantly help subsequent pageloads by sending 304 statuses when requested elements haven’t been modified.

FileETag MTime Size
ExpiresActive on
ExpiresDefault "access plus x seconds"

8. The easiest way to ban a WordPress spammer

To block certain IP address from accessing your blog enter the following code into .htaccess file and replace example IP address with the one you want to ban.

<Limit GET POST>
order allow,deny
deny from
allow from all

9. Quickly secure plugin files

WordPress plugin files might have a loop hole and may allow hackers to get into your website. To prevent others to have direct access to plugin files use following code.

<Files ~ ".(js|css)$">
  order allow,deny
  allow from all

10. Password protected directories

A simple way to password protect blog directories

AuthType Basic
AuthName "restricted area"
AuthUserFile /usr/local/var/www/html/.htpasses
require valid-user

3 thoughts on “htaccess tips and tricks to prevent your wordpress site from hackers”

  1. I have collected 17 htaccess methods in this article which I have used or tested it before, and I think some of them are essential tricks and tips to protect your webserver against malicious attacks and other would able to perform The following code will prevent user to access your . htaccess file.

Leave a Reply

Your email address will not be published. Required fields are marked *