使用htaccess阻止访问除我的IP地址之外的一系列URL [英] Using htaccess block access to a range of URLs except for my IP address

查看:248
本文介绍了使用htaccess阻止访问除我的IP地址之外的一系列URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何阻止除我的IP地址以外的所有范围或网址。

How can I block a range or URL's for all but my IP address.

所以......

允许所有用户 http://mydomain.com/ *

阻止 http://mydomain.com/thisdirectoryandallcontents/ *除了我的IP

Block http://mydomain.com/thisdirectoryandallcontents/* to all but my IP

我从另一个网站可能有效,但我没有htaccess技能来适应它

I found some code from another site which might work but I don't have the htaccess skills to adapt it

Options +FollowSymlinks
RewriteEngine on

#urls to exclude
RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
RewriteCond %{REQUEST_URI} !/url-to-exclude-2$
RewriteCond %{REQUEST_URI} !^/images$
RewriteCond %{REQUEST_URI} !^/css$
RewriteCond %{REQUEST_URI} !^/$

#ip to allow access
RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11$

#send to root
RewriteRule .? /http://www.mydomain.com [R=302,L]


推荐答案

我有类似的要求,当我把我的页面(或部分)放在维护上时基本相同。 htaccess文件的内容如下所示:

I have a similar requirement and do basically the same when I put my page (or a part) on maintenance. The content of the htaccess file looks like this:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} /thisdirectoryandallcontents
RewriteCond %{REMOTE_ADDR} !=111.111.111.111
RewriteRule ^.*$ /maintenance.php [R=302,L]

我会根据您的要求逐步解释:

I will explain it step by step according to your requirements:


  1. 每个用户都应该能够访问主页 http://mydomain.com/ - 很酷,无需配置。

  2. 第一个RewriteCond检查,如果请求位于文件夹/ thisdirectoryandallcontents中。

  3. 第二个RewriteCond检查,如果IP地址是任何其他,则111.111 .111.111(这里你必须写你的IP地址)。

  4. 如果两个条件都适用,我们会将用户重定向到/maintenance.php。在这里,我通常向用户输出一条消息,该页面当前正在维护中。您可以显示一条消息,该用户无权访问/ thisdirectoryandallcontents。
    如果您愿意,也可以将用户重定向到维护页面,而不是将用户重定向到主页。

  1. Every user should be able to access the homepage http://mydomain.com/ - cool, no configuration needed.
  2. The first RewriteCond checks, if the request lies within the folder /thisdirectoryandallcontents.
  3. The second RewriteCond checks, if the IP address is any other then 111.111.111.111 (here you have to write your IP address).
  4. If both of the conditions apply, we redirect the user to /maintenance.php. Here I usually output a message to the user, that the page is currently under maintenance. You could display a message, that the user doesn't have access to /thisdirectoryandallcontents. Instead of redirecting the user to the maintenance page, you could also redirect him to the home page if you wish to.

这篇关于使用htaccess阻止访问除我的IP地址之外的一系列URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆