想要重定向除我之外的所有访问者

时间:2022-11-29 23:39:34

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)

基本上我即将开始在网站上工作,我想要一些我可以添加到我的.htaccess文件(或其他地方)的东西,它将像这个伪代码一样工作:(我的ip将代替127.0.0.1 )

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

Hopefully that makes sense...

希望这有道理......

4 个解决方案

#1


22  

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.

正如Andrew指出的那样,如果您重定向到同一个域,则%{REQUEST_URI}条件会避免无限循环。

As Xorax comments almost 9 years later:

正如Xorax近9年后的评论:

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"

你不应该使用REMOTE_HOST,它会在很多情况下失败。你应该使用REMOTE_ADDR。 Cf“REMOTE_HOST和REMOTE_ADDR之间的差异”

#2


3  

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.

这是我最终使用的解决方案,请注意它与VonC类似,只是如果您选择重定向到同一个域,则会导致无限循环。

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.

还应该注意,302是临时移动,应该使用而不是什么或301。

#3


2  

<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.

在你的wordpress ifmodule之前,这将重定向除了3个ip地址之外的所有人。

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

如果IP地址发生变化,您只需ftp到站点并编辑.htaccess文件即可。

#4


0  

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.

我已经通过采用基于IP的方法来限制访问,然后丢失我的IP地址的租约而被烧毁。

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.

当然你总是可以直接进入有问题的盒子并再次更改.htaccess文件,但是当你试图找出刚刚发生的事情时,5分钟的恐慌并不是很有趣,如果你不期待它发生的话。

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

我建议使用.htaccess(与htpasswd文件一起使用)来请求访问开发站点的凭据。

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

这方面的一个很好的例子是:http://aplawrence.com/foo-web/htaccess-authentication.html

#1


22  

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.

正如Andrew指出的那样,如果您重定向到同一个域,则%{REQUEST_URI}条件会避免无限循环。

As Xorax comments almost 9 years later:

正如Xorax近9年后的评论:

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"

你不应该使用REMOTE_HOST,它会在很多情况下失败。你应该使用REMOTE_ADDR。 Cf“REMOTE_HOST和REMOTE_ADDR之间的差异”

#2


3  

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.

这是我最终使用的解决方案,请注意它与VonC类似,只是如果您选择重定向到同一个域,则会导致无限循环。

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.

还应该注意,302是临时移动,应该使用而不是什么或301。

#3


2  

<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.

在你的wordpress ifmodule之前,这将重定向除了3个ip地址之外的所有人。

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

如果IP地址发生变化,您只需ftp到站点并编辑.htaccess文件即可。

#4


0  

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.

我已经通过采用基于IP的方法来限制访问,然后丢失我的IP地址的租约而被烧毁。

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.

当然你总是可以直接进入有问题的盒子并再次更改.htaccess文件,但是当你试图找出刚刚发生的事情时,5分钟的恐慌并不是很有趣,如果你不期待它发生的话。

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

我建议使用.htaccess(与htpasswd文件一起使用)来请求访问开发站点的凭据。

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

这方面的一个很好的例子是:http://aplawrence.com/foo-web/htaccess-authentication.html