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

时间:2019-04-17 04:23:13

标签: apache .htaccess

如何在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?谢谢。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

def terrain(surface):
    surface = raw_input("What surface will you be driving on? ")
    while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
        surface = raw_input("Surface not valid. Please enter a valid surface: ")
    if surface == "ice":
                u = raw_input("what is the velocity of the car in meters per second? ")
                u = int(u)
                while int(u) < 0:
                    u = raw_input("Velocity must be a positive integer: ")
                                while int(u) == 0:
                                    u = raw_input("Velocty must be a number greater than zero: ")
                        while int(u) > 72:
                                                                u = raw_input("This vehicle cannot reach this speed: ")
                a = raw_input("How quickly is the vehicle decelerating? ")
                a = int(a)
                while int(a) > 0:
                    a = raw_input("Deceleration cannot be a positive integer: ")
                else: 
                        s1 = u**2
                        s2 = 2*.08*9.8
                    s = s1/s2
                    print "This is how far the vehicle will travel on ice: "
                    print ("The vehicle will travel %i meters before coming to a complete stop" % (s))
terrain("ice")

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

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