PHP:如果在.htaccess中直接访问任何文件路径,如何重定向


PHP: How to redirect if any file path is accessed directly in .htaccess

这是我有安全详细信息的路径,例如,
http://localhost/application/secure_details

secure_details是我的机密文件上传的文件夹
知道上述URL的第三方可以获得特定的详细信息,
这是不允许的。如果用户直接访问这个特定的URL,我需要重定向到error_page

使用mod_rewrite,将其添加到根.htaccess:

RewriteEngine on
RewriteBase /
RewriteRule ^application/secure_details/?.*?$ /error.html [L,R=301]

此外,将此.htaccess文件添加到secure_details目录:

Deny from all

首先需要在apache中启用rewrite模块来使用htaccess。之后,您可以使用以下脚本来确保您的url的安全

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^/*application/secure_details error.php[L,NC,QSA]

error.php文件中,您可以编写错误消息