允许从只有1个IP地址访问所有文件和重定向所有其他为其他文件文件、为其、重定向、地址

2023-09-02 09:43:29 作者:当你把自己的窗子打开,整个世界都会向你扑面而来。小编今天为朋

我不知道,如果在此之前已经回答了,但我试图寻找它。不管怎么说,我目前正在开发一个网站,但我想提出的实际网站的内容只能从我的IP地址进行访问。那么我想的.htaccess所有其他IP地址重定向到我的服务器上一个单独的文件。那一个文件将被称为 subscribe.php

I'm not sure if this has been answered before but I tried looking for it. Anyways, I'm currently developing a website but I would like to make the actual site content only accessible from my IP address. I then want .htaccess to redirect all other IP address to a separate file on my server. That one file would be called subscribe.php.

我已经试过几件事情,但没有给我提供了我想要的结果。我知道我的服务器允许的.htaccess 来使用,因为我用它来改变一些其他的东西,如preventing缓存。

I've tried a couple things but nothing provided me with the result I wanted. I know my server allows .htaccess to be used since I've used it to change some other things such as preventing caches.

推荐答案

您可以使用 的mod_rewrite 做到这一点。添加以下在的.htaccess 文件:

code:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule index.php$ /subscribe.php [R=301,L]

另一种解决方案:

<?php $allow = array("123.456.789", "456.789.123", "789.123.456"); //allowed IPs

if(!in_array($_SERVER['REMOTE_ADDR'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow)) {

    header("Location: http://domain.tld/subscribe.php"); //redirect

    exit();

} ?>

希望这有助于!

Hope this helps!