Search code examples
.htaccessmod-rewritehttp-status-code-302

Want to redirect all visitors except for me


Basically I'm about to start work on a site and I'd like something that I can add into my .htaccess file (or elsewhere) that'll work like this pseudo code: (my ip will be in place of 127.0.0.1)

if (visitors_ip <> 127.0.0.1)
    redirectmatch ^(.*)$ http://www.example.com/under-construction.html

Hopefully that makes sense...


Solution

  • That would be something like:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
    
    RewriteCond %{REQUEST_URI} !/mypage\.html$  
    
    RewriteRule .* http://www.anothersite.com/mypage.html [R=302,L]
    

    As Andrew points out, the %{REQUEST_URI} condition avoids infinite loop if you redirect to the same domain.

    As Xorax comments almost 9 years later:

    You should not use REMOTE_HOST, it will fail in many case. You should use REMOTE_ADDR.
    Cf "difference between REMOTE_HOST and REMOTE_ADDR"