【问题标题】:Kohana modules -path .htaccess protection and media -filesKohana 模块 -path .htaccess 保护和媒体文件
【发布时间】:2011-06-30 09:36:57
【问题描述】:

Kohana 中有modules -.htaccess 中的路径保护

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

我怎么能允许这样的路径:

http://localhost/modules/mymodule/media/js/myjavascript.js

我想在我的模块中包含 javascript 和其他媒体文件,并且仍然保护其他模块文件,例如 .php

我可以允许整个modules -path,但是所有.php -files 也会被列出。

# Protect application and system files from being viewed
RewriteRule ^(?:application|system)\b.* index.php/$0 [L]

当然有基本的 PHP 保护,但我仍然不希望任何人列出我的 modules 路径。

<?php defined('SYSPATH') or die('No direct script access.');

【问题讨论】:

    标签: .htaccess mod-rewrite module kohana


    【解决方案1】:

    最好的解决方案是使用媒体控制器提供这些文件。因此,用户可以请求“js/script.js”,Kohana 将使用级联文件结构加载它找到的第一个文件。 Kohana 附带了一个很好的媒体控制器,它在 Userguide 模块中:

    Line 247 of classes/controller/userguide.php

    public function action_media()
    {
        // Get the file path from the request
        $file = $this->request->param('file');
    
        // Find the file extension
        $ext = pathinfo($file, PATHINFO_EXTENSION);
    
        // Remove the extension from the filename
        $file = substr($file, 0, -(strlen($ext) + 1));
    
        if ($file = Kohana::find_file('media/guide', $file, $ext))
        {
            // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
            $this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request);
    
            // Send the file content as the response
            $this->response->body(file_get_contents($file));
    
            // Set the proper headers to allow caching
            $this->response->headers('content-type',  File::mime_by_ext($ext));
            $this->response->headers('last-modified', date('r', filemtime($file)));
        }
        else
        {
            // Return a 404 status
            $this->response->status(404);
        }
    }
    

    这不是最快的解决方案,但如果您正确设置响应标头,文件应该会缓存在客户端浏览器上。

    【讨论】:

      【解决方案2】:

      解决方法,在 RewriteRule 之前添加这个 RewriteCond

      # Protect application and system files from being viewed
      RewriteCond %{REQUEST_URI} !^(.*/)*(application|application/cache|modules/[^/]*)/media/.*$
      RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-18
        • 2021-03-20
        • 2017-02-06
        相关资源
        最近更新 更多