I recently implemented a way to limit access by IP range on specific blogs on WordPress MU. As you know, WordPress MU uses Apache Rewrite engine to rewrite URLs. For instance, you have a blog on WordPress MU called “intranet”. Apache Rewrite takes the “intranet” string in the URL and automatically rewrites it as a value in the PHP script. A side effect to Apache Rewrite is that “Directory” .htaccess parameters don’t work. So, if you wanted only your company IPs to access an internal blog, you will need to use Apache Rewrite parameters instead.
Here’s how to limit access to an IP or subnet on a particular blog on your WordPress MU install:
DISCLAIMER: Modifying .htaccess files can break your WordPress MU install. ALWAYS backup your .htaccess file. Simply, copy .htaccess file and rename it to .htaccess-backup. (cp .htaccess .htaccess-backup)
Step 1:
Add a section after the “RewriteEngine On
RewriteBase /…” section
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.5
RewriteCond %{REQUEST_URI} ^/BLOG1
RewriteRule .* - [F]
DO NOT SAVE YET. You will need edit the IP address info and blog info first:
Step 2: Replace the 1.2.3.4 number with your company IPs (its easier if you have an entire subnet or you can use internal IPs)
For a class C, the part after {REMOTE ADDR} would be
!^1\.2\3\.
Step 3: Replace BLOG1 with the blog you want to limit access to those IPs. So http://www.mysite.com/myfirstblog would be “^/blogs/
Optional: If you have WordPressMU installed in a folder (e.g. not your root folder), you will need to append the directory in front of the blog name.
For instance, if you have http://mysite.com/blog (as your WordPress MU root folder) the ^/ BLOG1 would be
^/blog/BLOG1
Optional: Multiple blogs with same access restrictions
By default, the Apache Rewrite treats every line as an AND statement. If you have multiple blogs, you will need to have an [OR] at the end of the line.
RewriteCond %{REQUEST_URI} ^/BLOG1 [OR]
RewriteCond %{REQUEST_URI} ^/BLOG2
Errors?
- Make sure you have ^/ marks in front of the blog names
- IP addresses must have a backslash before each dot. Regular expression for dot is concatenate by default, so it needs to be escaped
- Make sure you don’t forget the !^ sign before the IP, otherwise you will be forbidden.
If all else fails, if can’t fix the error, just copy back the .htaccess-backup to the .htaccess file.