.htaccess或VirtualHost,以阻止直接IP访问网站,但仅允许从URL访问

o2rvlv0m  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(139)

我阻止了通过虚拟主机使用IP直接访问我的网站。但是,我无法使用定义为error.php的403错误模板
由于error.php位于/var/www/html/error.php,因此会自动封锁,并错误输出Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

下面是我的000-default.conf

ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

<VirtualHost *:80>
        ServerName subdomain.example.com
        ServerAlias www.subdomain.example.com
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
        ServerName 1.1.1.1
        ServerAlias 2001:2001:2001:2001:2001:2001:2001:2001
        DocumentRoot /var/www/html
        <Location />
            Require all denied
        </Location>
</VirtualHost>

所以后来我试着用.htaccess来实现同样的效果(当然是在从000-default.conf中删除了这个块之后),我遇到了同样的错误。不能选择自定义的403模板,因为路径似乎被阻塞了。下面是.htaccess的内容放在/var/www/html/

RewriteCond %{HTTP_HOST} ^1\.1\.1\.1$
RewriteRule .* - [F,L]
#RewriteCond %{HTTP_HOST} !^2001\:2001\:2001\:2001\:2001\:2001$

运行Apache/2.4.38

gwo2fgha

gwo2fgha1#

设法整理出来。分享它是否可以帮助面临类似问题的人。

<VirtualHost *:80>
        ServerName 1.1.1.1
        ServerAlias 2001:2001:2001:2001:2001:2001:2001:2001
        DocumentRoot /var/www/html
        <Directory /var/www/html>
            Order deny,allow
            Deny from all
            Options FollowSymLinks
            <Files error.php>
                Order allow,deny
                Allow from all
            </Files>
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

相关问题