Hardening Wordpress with Mod Rewrite and htaccess

Reaper-X | Sep, 1 2007 Comment6 Comments RelatedRelated Posts Bookmark and ShareShare RSS SubscribeRSS Feed

It’s been a while since the last time i wrote tips or whatever you called it :P … so i decided to write some tips on how to hardening your wordpress installation. As as you already guessed, this tips might be useful if you’re using Wordpress as your platform (blog, etc)

But first as a reminder, this is not a perfect way to secure your wordpress installation (because of various reasons / factors), but at least it’s going to harden your wordpress installation

And as a note, this is tested only on Apache Web server, and you’re allowed to override the configurations using .htaccess file and lastly … you need to have mod rewrite installed and activated (don’t worry if you can use Custom Permalinks that mean you’ve mod rewrite enabled on your hosting / server)

Okay without further ado … here are the tips :

1. Disable Directory Indexing

By disabling directory indexing, you can prevent people seeing through your directory structure

Step to Disable it :

Create or edit .htaccess file on your public_html directory and add this into your htaccess :

Options -Indexes

2. Removing Wordpress version number

Some people think that hiding your wordpress version number is not necessary, so feel free if you want to implement it or not

Step to Remove it :

Remove this line from the header.php file of your currently used theme :

<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" />

also if you’re feeling a little bit adventurous, you can also remove the generator line from wp-includes/feed-*.php. Because by default wordpress is going to show your wordpress version number from your blog feed

3. Protecting WP-Admin Directory

In this example i’m going to tell you how to protect your wp-admin directory by checking your IP Address (it can be used even on dynamic ip address)

Step to Protect it :

3.1 If you’re using Static IP Address

Create new .htaccess file inside your wp-admin directory and add these lines into the newly created .htaccess file


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.0
RewriteRule .* http://www.domain.com/ [R,L]
</IfModule>

3.2 If you’re using Dynamic IP Address

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123\.456\.
RewriteRule .*
http://www.domain.com/ [R,L]
</IfModule>

What the above configuration does, is checking your IP Address first to see if you’re allowed to any file inside the wp-admin directory or not and if you’re not allowed to view it, you’ll be redirected automatically to specified address (you can use any address) by using 302 Found Header (Temporarily Moved)

As a side note, if you want the easiest way to password protect your wp-admin directory you can use this plugin by askapache or if you prefer to do it using the manual way … you can check blogsecurity website

4. Protecting wp-login.php and wp-register.php or any file you choose

Now you’ve protected your admin directory … but there’s one more step to do, especially if you’re the only person on your blog and also disabling user registration.

So this time we’re going to protect wp-login.php and wp-register.php, and here are the step to protect it :

Step to do it :

– Open the .htaccess file on the root of your public_html directory and choose which method that you like

4.1 Deny Access to wp-login.php by showing forbidden message


<Files wp-login.php>
Order deny,allow
Deny from All
Allow from 123.456.789.0
</Files>

4.2 Deny Access to wp-login.php by redirecting it to the specified address :


<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} wp-login.php
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.0
RewriteRule .* http://www.domain.com/ [R,L]
</IfModule>

As a note, you can also use Dynamic IP Address just like on Protecting WP-Admin Directory Method

That’s it … you’re done … hope this small wordpress tips can be useful for you

Bookmark and Share

Leave a comment

Comments

  1. TheahdimeLome Aug 16, 2010 at 9:54 AM

    i’m new… hope to brief nearly more regularly!

  2. John google.com/profiles/john.greer.tx Apr 30, 2009 at 8:35 AM

    If you have 2 or more people authoring the blog in multiple IPs, for tip #3 above, you can add multiple IPs with the htacess OR operator, for example:

    RewriteCond %{REMOTE_ADDR} !^70\.30\.200\.40|200.180.20.250

  3. mikle bigbones.com Aug 26, 2008 at 7:50 PM

  4. Martin Ankerl martin.ankerl.com Jan 25, 2008 at 12:09 AM

    Thanks a lot! My wordpress was recently hacked, now I have upgraded to the latest release and done all your suggestions. I hope this helps

  5. Reaper-XReaper-X Sep 5, 2007 at 2:58 PM

    Thanks Jonathan, beside i love reading askapache too :)

  6. Jonathan jonlandrum.com/ Sep 5, 2007 at 3:24 AM

    Nice! A lot of useful tips here. And thanks for the link to the AskApache plugin.

Trackbacks/Pingbacks

  1. 'How To Guide' for securing WordPress and protecting websites. | MileHighTechGuy > Productivity Tools & Technology
  2. 26个用于Wordpress的 .htaccess 规则 - 候鸟博客
  3. New Plugin: Integrity for WordPress ↔ BraveNewCode Inc.
  4. ‘How To Guide’ for securing WordPress and protecting websites. | MileHighTechGuy
  5. How to Improve WordPress Security | Interconnect IT - WordPress Consultants, Web Development and Web Design
  6. A to Z of WordPress .htaccess Hacks | Nometech.com
  7. Ultimate .htaccess file Examples
  8. Installing a LAMP Server, with Wordpress, on Slicehost (and maybe elsewhere) « A Life of Constant Flux
  9. Installing a LAMP Server on Slicehost (and maybe elsewhere) « A Life of Constant Flux
  10. Reading list, Virtual online worlds and MMOGs
  11. My favorite WordPress Resources | sebthom.de

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>