20 htaccess hacks to prevent your WordPress site from hacking
A .htaccess (hypertext access) file is the common name of a directory-level configuration file which allows decentralised 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.
A .htaccess file can be used for lots of hacks that will secure and improve functionality for WordPress blogs and websites. Below are lists of top 20 htaccess hacks which will improve and prevent WordPress sites and blog from hacking. Some will allow to block specific IP addresses to visit the site, redirect visitors to maintenance page when particular site is redesigned or modified, prevent IP addresses to login into the wordpress admin section and many more.
1. Blacklist undesired users and bots ip address
Apache can be used to ban undesirable people and bots from your website. This code allows people to visit the blog except the person with the IP addresses
<Limit GET POST PUT> 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 </LIMIT>
2. Redirect Day and name permalinks to /%postname%/
The first thing to do is to login to your WordPress admin, go to Settings → Permalinks and select custom. Fill out the field with /%postname%/.
Your permalinks will now look like the ones on this blog:
http://www.yourblog.com/name-of-the-post
Now we got to redirect all backlinks using the old permalinks structure to the new permalink structure. To do so, you’ll have to edit the .htaccess file, located in WordPress root directory.
Be careful while editing .htaccess: Always create a backup before!
Paste the following line in your .htaccess:
RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.domain.com/$4
3. Redirect visitors to a maintenance page
RewriteEngine on RewriteCond %{REQUEST_URI} !/maintenance.html$ RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123 RewriteRule $ /maintenance.html [R=302,L]
4. Redirect www to non www or vice versa
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www.yourblogname.com [NC] RewriteRule ^(.*)$ http://yourblogname.com/$1 [L,R=301]
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^yourblogname.com [NC] RewriteRule ^(.*)$ http://www.yourblogname.com/$1 [L,R=301]
5. Setting canonical url manually using .htaccess
# Set the canonical url RewriteEngine On RewriteCond %{HTTP_HOST} ^yourblogname\.com$ [NC] RewriteRule ^(.*)$ http://www.yourblogname.com/$1 [R=301,L]
6. Redirect WordPress Feeds to FeedBurner
This nice hack redirects http://www.yoursite.com/feed to http://feeds.feedburner.com/yoursite.
# temp redirect wordpress content feeds to feedburner <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC] RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC] RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/webanddesigners [R=302,NC,L] </IfModule>
7. Redirect WordPress Comment Feeds to FeedBurner
# temp redirect wordpress comment feeds to feedburner <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC] RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC] RewriteRule ^comments/feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/webanddesigners [R=302,NC,L] </IfModule>
8. SEO Friendly 301 Redirects
Use the following code to: Redirect to specific page without showing old fashion error page.
#SEO Friendly 301 Redirects Redirect 301 /abc/file.html http://www.yourblogname.com/def/file.html
9. 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"
10. 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 <LIMIT GET> order deny,allow deny from all allow from xx.xx.xx.xx </LIMIT>
11. How to: Deny comment posting to no referrer requests
Simple hack to prevent spammers posting on your blog.
RewriteEngine On RewriteCond %{REQUEST_METHOD} POST RewriteCond %{REQUEST_URI} .wp-comments-post\.php* RewriteCond %{HTTP_REFERER} !.*yourblog.com.* [OR] RewriteCond %{HTTP_USER_AGENT} ^$ RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
12. 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.
## USER IP BANNING <Limit GET POST> order allow,deny deny from 200.49.176.139 allow from all </Limit>
13. Redirect visitors to a maintenance page
RewriteEngine on RewriteCond %{REQUEST_URI} !/maintenance.html$ RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123 RewriteRule $ /maintenance.html [R=302,L]
14. Deny access to your wp-config.php file.
wp-config.php file in WordPress includes all important information like database name,
# protect wpconfig.php <files wp-config.php> order allow,deny deny from all </files>
15. Limit the File upload size to 20MB
To limit file upload size in wordpress to 20MB use the following code.
# limit file uploads to 10mb LimitRequestBody 10240000
16. Customized HTTP 404 error page
If you’d like to redirect your visitors every time they catch into an HTTP 404 error, use this code:
# custom error pages ErrorDocument 401 /err/401.php ErrorDocument 403 /err/403.php ErrorDocument 404 /err/404.php ErrorDocument 500 /err/500.php
17. Add Trailing Slash to URL
To add slash at the end of your URL add the following code to .htaccess file.
#trailing slash enforcement RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !# RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
18. 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
19. CheckSpelling directive
This directive can be useful to auto-correct simple spelling errors in the URL
<IfModule mod_speling.c> CheckSpelling On </IfModule>
20. 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 </Files>
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. Do leave your comment if you have come across hacks like these.