首页 > 解决方案 > 如何在 apache 2.4 上将 403 页面伪装成 404?

问题描述

如何在 apache 2.4 上将 403 页面伪装成 404?

这是我的目录结构,我使用codeigniter3。

document_root/
    admin/ <- codeigniter project
        application/
        bin/
        public/
            index.php
        vendor/
        .htaccess <- access restriction write to here.
        composer.json
    index.html <- front page

我想拒绝来自未经认证的 IP 地址的访问。返回响应码 404,而不是 403。(因为想要隐藏目录存在)

document_root/
    admin/ <- wanna return 404
        application/ <- wanna return 404
        bin/ <- 404
        public/ <- 404
            index.php <- 404
        vendor/ <- 404
        .htaccess  <- 404
        composer.json <- 404
    index.html <- return 200 (OK)

所以我把这段代码写到 .htaccess

### Define Environment Variables
<IfModule mod_env.c>
    SetEnv CI_ENV development
    SetEnvIf REMOTE_ADDR 192.168.33.1 IsAdmin=1
    #SetEnvIf X-Forwarded-For xx.xx.xx.xx IsAdmin=1
</IfModule>

### Access Restriction By Client IP Address
Order deny,allow
Deny from all
Allow from env=IsAdmin

ErrorDocument 403 /admin/

### Return 404 Error To Denied Clients (To Hide Directory Exists)
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{ENV:IsAdmin} !=1
    RewriteRule .* - [R=404,L]
</IfModule>

但是这段代码的影响是这样的。

document_root/
    admin/ <- 404 (OK)
        application/ <- 403 (NG)
        bin/ <- 403 (NG)
        public/ <- 403 (NG)
            index.php <- 403 (NG)
        vendor/ <- 403 (NG)
        .htaccess  <- 403 (NG)
        composer.json <- 404 (OK)
    index.html <- return 200 (OK)

我怎样才能将 403 页面伪装成我想要的 404?谢谢你。

标签: apache.htaccess

解决方案


我解决了这个问题。

### Define Environment Variables
<IfModule mod_env.c>
    SetEnv CI_ENV development
    SetEnvIf REMOTE_ADDR 192.168.33.1 IsAdmin=1
    #SetEnvIf X-Forwarded-For xx.xx.xx.xx IsAdmin=1
</IfModule>

### Access Restriction By Client IP Address
Order deny,allow
Deny from all
Allow from env=IsAdmin

### Replace 403 Page To 404
ErrorDocument 403 /admin/e403.html
ErrorDocument 404 /admin/e404.html

<Files ~ "e40(3|4).html">
    Order allow,deny
    Allow from all
</Files>

### Return 404 Error To Denied Clients (To Hide Directory Exists)
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{ENV:IsAdmin} !=1
    RewriteRule e403.html - [R=404,L]
</IfModule>

需要写404页。(不需要403页)

<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Not Found</h1>
<p>The requested URL /admin/ was not found on this server.</p>
</body>
</html>

推荐阅读