How to deny access to specific pages with htaccess

∥☆過路亽.° 提交于 2021-01-28 07:38:09

问题


How can I send a 404 page error for specific pages that exist on the web server? (Using htaccess).

For example, I have a config.php file and when I go to localhost/config.php it just shows a blank white page with nothing in it. I want it to appear as a 404 page, like the page doesn't exist.

How can I do that?

Thanks!


回答1:


If you specifically want to return a 404 Not Found then you can use mod_rewrite, near the top of your .htaccess file:

RewriteEngine On
RewriteRule ^config\.php$ - [R=404,L]

However, to "deny" access, and send a 403 Forbidden, then it would be preferable to use the following (core) directives on Apache 2.4:

<Files "config.php">
    Require all denied
</Files>

You can achieve the same with mod_rewrite by using the F (forbidden) flag. For example:

RewriteRule ^config\.php$ - [F]

(The L flag is not required in this instance, since it is implied.)



来源:https://stackoverflow.com/questions/43765007/how-to-deny-access-to-specific-pages-with-htaccess

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!