阻止查看某些 .php 文件

发布于 2024-10-03 22:13:29 字数 425 浏览 1 评论 0原文

我将 PHP 与 Apache 结合使用,并使用漂亮的 URL 与 mod_rewrite 结合使用。

我有一个 index.php,其中包含一些包含内容的其他 .php 文件。
现在我想阻止人们查看这些(仅内容).php 文件,除非它们是通过我的 index.php 脚本包含的。

这是我正在使用的重写规则:
RewriteRule ^/?([a-zA-Z0-9/-]+)/?$ /index.php [NC,L]

它将每个请求重写到处理请求的index.php并包含相应的内容。

有没有办法用mod_rewrite(即我的.htaccess)来做到这一点?通常拒绝访问 .php 文件或类似的文件?

谢谢!

I'm using PHP with Apache and pretty URLs with mod_rewrite.

I have an index.php where I include some other .php files that contain the content.
Now I'd like to prevent people from viewing those (content only) .php files, unless they are included via my index.php script of course.

This is the RewriteRule I'm using:
RewriteRule ^/?([a-zA-Z0-9/-]+)/?$ /index.php [NC,L]

It rewrites every request to the index.php where requests are handled and content is included accordingly.

Is there a way to do this with mod_rewrite, i.e. my .htaccess? To generally deny access to .php files or something like that?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-10-10 22:13:29

在索引文件中放入一个常量,然后在每个 php 文件中执行以下操作:

defined('ALLOWED') or die('No direct script access.');

Put a constant in you index file and then in each php file do something like:

defined('ALLOWED') or die('No direct script access.');
不再让梦枯萎 2024-10-10 22:13:29

将 PHP 文件(index.php 除外)保留在 Web 根目录之外。您仍然可以在代码中包含/要求它们,但人们将无法通过 URL 访问它们。

Keep your PHP files (except index.php) out of the web root. You can still include/require them from your code, but people won't be able to access them by URL.

嘦怹 2024-10-10 22:13:29

<罢工>
如果所有请求都重写到index.php,那么当前如何访问这些文件?您不需要拒绝访问。

使用

RewriteRule ^(.*)$ index.php [L]

和处理 PHP 中的其他所有内容。


How do you access these files currently if all requests are rewritten to index.php? You should not need to deny access.

Use

RewriteRule ^(.*)$ index.php [L]

and handle everything else in PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文