文件夹在htaccess中没有引发404错误

时间:2015-07-24 16:06:21

标签: php apache .htaccess mod-rewrite

简单地说,我试图这样做,如果用户转到某个文件夹,它会给出404 Error。问题是,它似乎不起作用!

这是我的文件设置:

- postin
    - mail
         - yourmail.php
    - profiles
        - viewprofile.php
    - .htaccess
    - error.php

然后我只是将其添加到我的.htaccess:

RewriteRule ^mail/ - [L,R=404]
ErrorDocument 404 error.php

我想通过添加此代码,如果用户键入URL .../postin/mail/,则会将用户带到404 error.php页面。但事实并非如此,它只会显示.../postin/mail/的目录页面。所以我的问题是,当用户进入文件夹(目录)时,如何引发404错误?

如果您想要我的完整.htaccess,请点击此处:

Options +FollowSymLinks
RewriteEngine on

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule search/(.+)/?$ search.php?query=$1 [NC,L]
RewriteRule search/?$ search.php [NC,L]
RewriteRule register/?$ registerpage.php [NC,L]
RewriteRule login/?$ loginpage.php [NC,L]
RewriteRule home/?$ home.php [NC,L]
RewriteRule users/?$ users.php [NC,L]
RewriteRule error/?$ error.php [NC,L]

RewriteRule ^mail/ - [L,R=404]
ErrorDocument 400 http://localhost/postin/error
ErrorDocument 401 http://localhost/postin/error
ErrorDocument 403 http://localhost/postin/error
ErrorDocument 404 http://localhost/postin/error
ErrorDocument 500 http://localhost/postin/error
ErrorDocument 502 http://localhost/postin/error
ErrorDocument 504 http://localhost/postin/error

<Files .htaccess>
order allow,deny
deny from all
</Files>

1 个答案:

答案 0 :(得分:1)

您可以使用:

DirectoryIndex /errorpage.php

这应该是你的htaccess中的第一行