【问题标题】:htaccess prevent direct page loadhtaccess 防止直接页面加载
【发布时间】:2013-02-02 19:36:23
【问题描述】:

我试图阻止使用 htaccess 直接访问我网站上除 index.php 之外的所有 php 文件。到目前为止,我已经有了以下代码,但是它仍然不能阻止表单直接访问 php 文件。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?page=$1 [L]
</IfModule>

我的解决方案:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|.*\.css|.*\.js|.*\.png|.*\.gif|.*\.jpg)
RewriteRule ^(.*)$ index.php?page=$1 [L]
</IfModule>

【问题讨论】:

  • 将您不打算直接访问的 PHP 文件放在服务器的 Web 根目录之外。

标签: php .htaccess


【解决方案1】:

这是因为以下几行:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

如果请求的资源是文件或目录,您明确告诉重写模块不要运行以下规则

但是,我同意最好将它们完全移出网络根目录。

【讨论】:

  • 但如果我删除这些行,我将无法加载像“style.css”这样的文件或任何图像。
  • 当然可以,您只需更改匹配逻辑以不重写所有文件,而只需更改以扩展名.php 结尾的文件。这是一项非常简单的任务 - 我认为您可以解决这个问题。
猜你喜欢
  • 2016-11-22
  • 2013-09-05
  • 2010-11-05
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
相关资源
最近更新 更多