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

发布于 2024-07-09 04:00:44 字数 252 浏览 9 评论 0原文

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

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

希望这是有道理的......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故事↓在人 2024-07-16 04:00:45
<IfModule mod_rewrite.c>
 RewriteEngine On
#IPv4
 RewriteCond %{REMOTE_ADDR} !^26\.122\.33\.214 
#IPv6
 RewriteCond %{REMOTE_ADDR} !^2a01\:3f0d\:e31e\:c410\:98b\:a6ff\:45b0\:5132
#avoid infinite loop
 RewriteCond %{REQUEST_URI} !/offline/index.html$ [NC]
#allow images to be accessed
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
#redirect to the offline page or other URL.
 RewriteRule .* /offline/index.html [R=302,L]
</IfModule>
<IfModule mod_rewrite.c>
 RewriteEngine On
#IPv4
 RewriteCond %{REMOTE_ADDR} !^26\.122\.33\.214 
#IPv6
 RewriteCond %{REMOTE_ADDR} !^2a01\:3f0d\:e31e\:c410\:98b\:a6ff\:45b0\:5132
#avoid infinite loop
 RewriteCond %{REQUEST_URI} !/offline/index.html$ [NC]
#allow images to be accessed
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
#redirect to the offline page or other URL.
 RewriteRule .* /offline/index.html [R=302,L]
</IfModule>
东京女 2024-07-16 04:00:44

这将是类似

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]

Andrew 指出,如果您重定向到同一域,%{REQUEST_URI} 条件可以避免无限循环。

正如Xorax评论将近 9 年后

您不应该使用REMOTE_HOST,它在很多情况下都会失败。 您应该使用REMOTE_ADDR
参见“REMOTE_HOSTREMOTE_ADDR 之间的区别”

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"

踏雪无痕 2024-07-16 04:00:44

这是我最终使用的解决方案,请注意,它与 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]

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

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.

往事随风而去 2024-07-16 04:00:44
<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>

在你的 WordPress ifmodule 之前
这将重定向除相关 3 个 IP 地址之外的所有人。

如果您的 IP 地址发生变化,您只需通过 ftp 访问该站点并编辑 .htaccess 文件即可。

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

初熏 2024-07-16 04:00:44

使用这种方法要小心。

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

当然,您始终可以 ssh 进入有问题的盒子并再次更改 .htaccess 文件,但是如果您没有预料到它会发生,那么当您试图弄清楚刚刚发生了什么时,这 5 分钟的恐慌并不完全有趣。

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文