阻止直接访问所有php文件,除了codeigniter中的一些文件

时间:2013-08-29 14:09:31

标签: .htaccess codeigniter

我想在我的服务器中的Codeigniter旁边安装另一个脚本。问题是Codeigniter默认包含一个.htaccess文件,下面有代码阻止直接访问php文件:

# Hide all PHP files so none can be accessed by HTTP
RewriteRule (.*)\.php$ index.php/$1

如何为此代码添加一些例外?! 如果我的PHP文件位于.htaccess所在文件夹之外的另一个文件夹中,我应该如何获得例外?!

我的整个代码如下:

# set it to +Indexes
Options -Indexes

Options SymLinksIfOwnerMatch

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>
    # mod_rewrite rules
    RewriteEngine on

    # If the file is NOT the index.php file
    RewriteCond %{REQUEST_FILENAME} !index.php
    # Hide all PHP files so none can be accessed by HTTP
    RewriteRule (.*)\.php$ index.php/$1

    # If the file/dir is NOT real go to index
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]

</IfModule>

# If Mod_ewrite is NOT installed go to index.php
<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>

2 个答案:

答案 0 :(得分:2)

添加另一个排除条件,如下所示:

# If the request is not from /some-folder/
RewriteCond %{REQUEST_URI} !^/some-folder/ [NC]
# If the file is NOT the index.php file
RewriteCond %{REQUEST_FILENAME} !index.php
# Hide all PHP files so none can be accessed by HTTP
RewriteRule ^(.*)\.php$ index.php/$1 [L,NC]

答案 1 :(得分:0)

这可能会有所帮助 - 将您的codeigniter文件夹放在html文件上方一级。 我将通过稍微重命名ci文件夹来显示这个

 /               /html/site files are here
/applicationbeta/html/
/systemci2      /html/ 

在网站文件中,创建一个文件夹,例如“网站” 将你的codeigniter index.php文件放入其中 将任何资产如css和js放入其中

              /html/website/index.php 
              /html/website/assets/

然后在index.php文件中更改系统和应用程序文件夹的路径 我已将它们重命名为显示制作不同版本的容易程度

$system_path = '../../systemci2'; 
$application_folder = '../../applicationbeta';  

在index.php中放置一个基本的url配置 - 它将处理网站文件夹,并使所有链接都正确。

$assign_to_config['base_url'] = 'http://domain.com/website/';

文件夹网站中的htaccess文件应包含类似

的行
RewriteRule ^(.*)$ /website/index.php/$1 [L]

这样做有很多好处。包括 - 不必担心开发与部署服务器的base_url()设置。

我刚刚扫描了一篇关于如何使用大量代码动态设置base_url的长篇教程。但是如果你只是在index.php文件中设置它 - 你永远不必担心它 - 因为index.php文件位置 - 永远不会改变!

相关问题