【问题标题】:htaccess is denying access to index.php as directory indexhtaccess 拒绝访问 index.php 作为目录索引
【发布时间】:2017-11-15 20:41:45
【问题描述】:

我的 .htaccess 文件:

DirectoryIndex index.php

order allow,deny
<FilesMatch "^(index|testfile)\.php$">
Allow From All
</FilesMatch>

拒绝访问站点根目录,这通常会打开 index.php。加载站点的唯一方法是转到 mysite.com/index.php 而不是 mysite.com 如果我删除 order 它会按预期工作,但现在该目录中的任何文件都可以访问。

如何配置 htaccess 以拒绝除index.php 之外的所有文件,但也允许导航到mysite.com 而不是mysite.com/index.php

【问题讨论】:

  • 反对者能否解释原因?

标签: php apache .htaccess


【解决方案1】:

你可以这样做:

DirectoryIndex index.php
order Deny,Allow

# deny everything except landing page
<FilesMatch ".">
Deny From All
</FilesMatch>

# allow index.php and testfile.php
<FilesMatch "^(index|testfile)\.php$">
Allow From All
</FilesMatch>

替代解决方案使用mod_rewrite

DirectoryIndex index.php
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !\.(?:css|js|png|jpe?g|gif|ico|tiff)$ [NC]
RewriteRule ^(?!(index|testfile)\.php). - [F,NC]

【讨论】:

  • 感谢您的帮助,看起来它仍然拒绝访问,除非我在网址中添加尾随 /,有什么办法吗?
  • 当我在我的 Apache 上进行测试时,它允许 http://localhosthttp://localhost/index.phphttp://localhost/testfile.php
  • 你也可以试试我的替代方案
  • 谢谢!重写规则允许我在没有斜杠的情况下访问,但现在我需要取消阻止对我所有 css 和 js 资产的访问:/ 我会问一个后续问题,因为这与这个问题无关。
  • 您可以使用RewriteCond,就像我在编辑后的答案中一样。
猜你喜欢
  • 2017-03-17
  • 1970-01-01
  • 2012-08-02
  • 2016-10-18
  • 2012-05-16
  • 2017-10-03
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多