问题
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