Want to redirect all visitors except for me

丶灬走出姿态 提交于 2019-12-02 19:28:34

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"

Here's the solution I ended up using, note that it is similar to VonC's except that his caused an infinite loop if you chose to redirect to the same domain.

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^127\.0\.0\.1
RewriteCond %{REQUEST_URI} !/coming-soon\.html$ 
RewriteRule .* http://www.andrewgjohnson.com/coming-soon.html [R=302,L]

It should also be noted that 302 is a temporary move and should be used instead of nothing or 301.

<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect all except allowed IP
ReWriteCond %{REMOTE_ADDR} !^000\.000\.000\.001$
RewriteCond %{REMOTE_ADDR} !000\.000\.000\.002$
ReWriteCond %{REMOTE_ADDR} !^000\.000\.000\.003$
RewriteRule (.*) http://YourOtherWebsite.com/$1 [R=301,L] 
</IfModule>

before your wordpress ifmodule this will redirect everyone except the 3 ip address in question.

You simply ftp to the site and edit the .htaccess file if your IP address changes.

Be careful with this approach.

I've gotten burned by taking the IP based approach to limiting access, and then losing the lease on my IP address.

Of course you can always ssh into the box in question and change the .htaccess file again, but the 5 minutes of panic while you try to figure out what just happened aren't exactly fun if you aren't expecting it to happen.

I recommend instead using the .htaccess (in conjunction with an htpasswd file) to request credentials for accessing your development site.

A good example of that is here: http://aplawrence.com/foo-web/htaccess-authentication.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!