【发布时间】:2015-12-01 21:39:40
【问题描述】:
我的 apache 服务器可以通过公共 IP 和指向该 IP 的域从 Internet 访问。
我想要实现的是:
-
当用户尝试使用域或公共 IP 访问服务器的根目录
/,因此http://example.com/或http://178.37.8.6/,他得到403 禁止 -
但是,当用户使用任何特定文件或目录的完整 URL 时,服务器照常运行(因此文件可访问,并且目录返回典型列表)
例如http://example.com/casual_directory/,http://example.com/awesome_file.html -
上述规则不适用当我使用本地服务器的 IP 地址从本地网络访问服务器时 -
192.168.44.100或10.44.0.1
我现在得到了什么:
RewriteEngine on
RewriteCond %{SERVER_NAME} !=192.168.44.100
RewriteCond %{SERVER_NAME} !=10.44.0.1
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^. - [F,L]
虽然我认为关于 ip 地址的两个条件可以正常工作,但我正在努力为根目录制定一个正确的规则。
我检查过,REQUEST_URI 和 REQUEST_FILENAME 都返回相同的根目录,即“/”
很可能我无法在那里编写正确的正则表达式。
我花了很多时间做研究,i.a. here、here、here、also here、here as well、even there、
here is a bit different case
但它们都没有按预期工作,有一次它无法访问任何 url,另一种则根本不起作用。
@edit 26.12.2015
几周后,我不得不重新启动我的服务器(这种情况并不经常发生),并注意到即使在我的本地网络中,access do root dir / 现在也被禁止(直到现在它按预期工作)。
我必须更正 the solution I mentioned in the comment bellow 所以现在我的配置是这样的:
<DirectoryMatch "^/var/www/$">
Require all denied
Require ip 192.168.44.0/24
Require ip 10.44.0.0/24
</DirectoryMatch>
【问题讨论】:
标签: regex apache .htaccess mod-rewrite http-status-code-403