簡體   English   中英

.htaccess 或 VirtualHost 阻止網站直接 IP 訪問,但僅允許來自 URL

[英].htaccess or VirtualHost to block website from direct IP access, yet allow from URL only

我已阻止通過虛擬主機使用 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 模板,因為路徑似乎已被阻止。 以下是位於/var/www/html/的 .htaccess 內容

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

運行 Apache/2.4.38

設法整理出來。 分享它是否可以幫助面臨類似問題的人。

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM