apache - 如何在 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?谢谢你。
解决方案
我解决了这个问题。
### 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>
推荐阅读
- javascript - Javascript:获取用户名列表到控制台/文件
- angular - 角度谷歌地图方向 - 如何绑定起点和目的地
- mongodb - 如何在 MongoDB 中使用一个查询更新多个文档
- laravel - Laravel 主键增量在 eloquent 和迁移中
- sql - SQL 创建表和数据
- php - 如何使用 php 和 mysql 使用谷歌图表创建堆积条形图?
- php - PHP 默认 Post 值
- react-native - 从横向旋转到纵向时,Android 应用程序显示在屏幕的一半上
- powershell - 在 PowerShell 控制台中捕获和打印 7-Zip 的提取进度
- php - Wamp:当文件不在“所有人”的“完全控制”中时,权限被拒绝(Windows)