【问题标题】:Simulating MVC/CoC folder structure in php with mod_rewrite使用 mod_rewrite 在 php 中模拟 MVC/CoC 文件夹结构
【发布时间】:2014-04-07 01:26:04
【问题描述】:

我的项目目录结构:

project_root/
|- src/
|  |- model/
|  |- view/
|  |- controller/
|  -- ...others
-- resources/
   |- css/
   |- js/
   -- images/

好的,在src的某处有一个文件负责重定向控制器、视图等,假设它是src/url_mapping.php,它准备解决任何类型的url,例如/{controller}/{action}/{id}?{query},这里没有问题,重要的是我不想让用户意识到这个文件夹结构的存在,我想对他们隐藏它,让流程像 RoR 和其他基于系统的系统一样简单。

我希望只允许来自resourceshttp://host/{css,js,images}/* 而不允许http://host/resources/* 本身,以及http://host/{controller}/{action}/{id}?{query}src/url_mapping.php 文件的拒绝直接访问http://host/src

目前,我管理的最接近的是以下.htaccess 文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{DOCUMENT_ROOT}/resources/$1 !-f
    RewriteCond %{DOCUMENT_ROOT}/resources/$1 !-d
    RewriteCond %{DOCUMENT_ROOT}/resources/$1 !-l
    RewriteRule (?!^resources/|^src/url_mapping.php/)^(.*)$ src/url_mapping.php/$1 [L,PT]

    RewriteCond %{DOCUMENT_ROOT}/resources/$1 -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/resources/$1 -d [OR]
    RewriteCond %{DOCUMENT_ROOT}/resources/$1 -l
    RewriteRule (?!^resources/)^(.*)$ /resources/$1 [L,PT]
</IfModule>

在这种情况下,我的第一个意图是:如果用户尝试获取 /resources/src 文件夹,它也会被 src/url_mapping.php 过滤,尽管我尝试了几种组合,但一次又一次没有成功。

我注意到mod_rewrite 处于循环状态,因此我无法阻止resources 目录以及src/url_mapping.php,我被束缚在.htaccess 文件中,但如果有其他方法可以做我会接受解决方案。

最后的选择是只做一次重定向,并通过src/url_mapping.php 处理resources 中的偶数文件,但它会杀死缓存,我避免重新发明轮子。

【问题讨论】:

标签: php apache .htaccess mod-rewrite


【解决方案1】:

好吧,我意识到 apache 并不那么聪明,所以我发现一个可以接受的解决方案是更改目录树以隐藏非资源内容,这样 apache 无法访问。

想象DocumentRoot/home/user/www,所以如果你浏览到http://hostname/somefile 将指向/home/user/www/somefile,这样/home/user/somedir/somefile 中的文件不能被apache 访问,但可以被你的PHP 脚本访问,如果它对该目录拥有所有权/权限。

所以,resources 中的所有内容我都在 /home/user/www 上,src/home/user/private_html 上,只有我的 url_mapping.php 留在 /home/user/www/index.php 上,这样我可以在我抛出 @987654332 时愚弄用户@直接访问时。

最后一棵树:

project_root
|- private_html
|  |- model
|  |- view
|  |- controller
|  -- ...others
-- public_html
   |- css
   |- js
   |- images
   -- index.php

我的.htaccess 文件在哪里:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase / # Change it if there are a subdirectory

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

而我的index.php 是:

<?php

if (!array_key_exists('REDIRECT_URL', $_SERVER) && substr($_SERVER['REQUEST_URI'], 0, strlen($_SERVER['SCRIPT_NAME'])) == $_SERVER['SCRIPT_NAME']) {
    header('HTTP/1.0 404 Not Found');
    exit;
}

define('VALID_KERNEL', true);

require '../private_html/core/core.php';

$server = new Server();
$server->request(array_key_exists('PATH_INFO', $_SERVER) ? $_SERVER['PATH_INFO'] : '/', $_REQUEST);

?>

这种方式允许访问index.php,只有当它是DirectoryIndex时,我的意思是,http://hostname/是允许的,但http://hostname/index.php404 Not Found,因为这个文件不应该存在,以及http://hostname/index.php/anythinghttp://hostname/index.php?anything,虽然http://hostname/anything 会重定向到http://hostname/index.php/anything,但使用REDIRECT_URL 是允许的。

【讨论】:

    猜你喜欢
    • 2011-03-09
    • 2015-03-07
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    相关资源
    最近更新 更多