【问题标题】:Block direct access to all the php files except some of them in codeigniter阻止对除 codeigniter 中的一些文件之外的所有 php 文件的直接访问
【发布时间】:2013-08-29 14:09:31
【问题描述】:

我想在我的服务器中在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>

【问题讨论】:

    标签: .htaccess codeigniter


    【解决方案1】:

    像这样添加另一个排除条件:

    # 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]
    

    【讨论】:

    • 文件多了怎么办?!例如我有index2.php,那么我必须为每个人添加一行?!或者我可以通过将| 放在每个文件名旁边来做到这一点?!
    • 是的,您可以像这样使用 | 的 OR 条件:RewriteCond %{REQUEST_FILENAME} !(index|index2)\.php
    【解决方案2】:

    这可能会有所帮助 - 将您的 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/';
    

    文件夹 website 中的 htaccess 文件应该包含类似

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 2014-01-08
      • 2014-01-16
      • 2014-06-04
      • 1970-01-01
      相关资源
      最近更新 更多