.htaccess and wordpress 301 redirect everyone not on the approved list of IP addresses?

Is there a cleaner way to do this?
I’m interested in updating the file with a list via a script that will run periodically with allowed IP addresses.

the logic is:
If {REMOTE_ADDR} = (1.2.3.4|2.3.4.5|3.4.5.6|192.168.0.0/24)
do nothing
Else
Redirect them 301 to the current website.

Read More

I’ve tried the following test:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress

    # Redirect all except allowed IP
    RewriteCond %{REMOTE_ADDR} !^123.456.789.101$
    ReWriteCond %{REMOTE_ADDR} !^234.567.891.011$
    RewriteRule (.*) http://Currentwebsite.com/$1 [R=301,L] 

Related posts

Leave a Reply

1 comment

  1. You need the redirect stuff to happen before your wordpress rules. Because wordpress routes everything to index.php, the request gets mangled before the redirect:

    # Redirect all except allowed IP
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REMOTE_ADDR} !^123.456.789.101$
    ReWriteCond %{REMOTE_ADDR} !^234.567.891.011$
    RewriteRule (.*) http://currentwebsite.com/$1 [R=301,L] 
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress