如何通过 .htaccess 将除某些 IPS 之外的所有用户重定向到维护页面



我是.htaccess的新手,无法根据需要在.htaccess中编写条件。我已经尝试了许多其他用户共享的类似解决方案,但问题仍未解决。所以这就是为什么我把我的问题分开放在这里。

我有一个网站,但几天来,它进入维护模式。为此,我在根目录中创建了 maintainence.php 文件。因此,如果任何用户点击此域下的任何URL,那么他们将收到以维护.php文件编写的消息。

但是我想允许一些IP访问整个网站,域下的任何URL。我正在尝试使用 .htaccess 做这件事。我认为使用 .htaccess 文件执行此操作是最好的方法。

我尝试过的内容如下:

<IfModule mod_rewrite.c>
   Options +FollowSymlinks
   RewriteEngine on
   RewriteCond %{REMOTE_ADDR} ^xxx.xx.xx.xx$ #this is IP that I want to allow
   RewriteCond %{REQUEST_URI} !=/maintainence.php
   RewriteRule ^(/.*)?$ /maintainence.php [R=301,L]
</IfModule>  

上述解决方案将所有人重定向到维护.php包括提到的IP。

谁能帮我解决这个问题?提前致谢

你几乎否认IP不允许它,所以如果你想允许一些IP并阻止其他IP只是像这样修复你的代码:

 <IfModule mod_rewrite.c>
 Options +FollowSymlinks
 RewriteEngine on
 RewriteCond %{REMOTE_ADDR} !^xxx.xx.xx.xx [OR]
 RewriteCond %{REMOTE_ADDR} !^xxx.xx.xx.xx [OR]
 RewriteCond %{REMOTE_ADDR} !^xxx.xx.xx.xx 
 # i just excluded three but you could do less or more 
 RewriteCond %{REQUEST_URI} !=/maintainence.php
 RewriteRule ^(.*)$ /maintainence.php [R=301,L]
</IfModule> 

注意:清除浏览器缓存测试它。

相关内容

最新更新