将htaccess配置为只读index.php

时间:2012-06-19 09:54:27

标签: php apache .htaccess

他们是否可以将.htaccess文件配置为只读特定文件?只说index.php而不是其他人吗?

编辑:我知道index.php是将要读取的默认文件。我想要这样的东西:

如果用户输入www.example.com,则默认情况下会读取index.php。 如果输入www.example.com/index2.php,则应该读取index2.php。

不是我想要的是无论网址是什么,都会显示404错误页面。我只有两个文件应该被阅读。

2 个答案:

答案 0 :(得分:3)

Zend Framework提供了一个默认的.htaccess,它将所有请求路由到index.php,除非请求的文件确实存在:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

您可以使其更具限制性并将所有内容路由到index.php:

RewriteEngine On
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

请记住,这会对CSS文件提出额外的请求。

[编辑]

看看Drupal提供的.htaccess也很不错。在那里,某些路径和文件类型也被阻止。 Drupal的.htaccess中的第一个指令是:

<FilesMatch "\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$">
    Order allow,deny
</FilesMatch>

所以,如果你想阻止只访问php文件和application目录中的所有内容,你可能会这样做:

<FilesMatch "(\.php$|^application)">
    Order allow,deny
</FilesMatch>

(未经过测试,因为我不是htaccess专家而且代码不在我的脑海;如果需要更正,请有人在评论中发表评论或直接编辑我的代码)。

答案 1 :(得分:0)

您还没有真正提供有关您真正想要做的事情的更多信息。如果您尝试仅对某些文件进行身份验证,那么您可以使用如下所示的内容:

<Files "index.php">
require valid-user
</Files>

不幸的是,如果没有更多信息,我无法提出任何其他建议。