apache - Simulating MVC/CoC folder structure in php with mod_rewrite -
my project directory structure:
project_root/ |- src/ | |- model/ | |- view/ | |- controller/ | -- ...others -- resources/ |- css/ |- js/ -- images/ ok, somewhere in src there file responsible redirecting controllers, views , on, let src/url_mapping.php, prepared solve kind of url, such /{controller}/{action}/{id}?{query}, there no problem here, big deal not want user realize folder structure exists, want hide them , let flow simples ror , other based systems.
i want allow http://host/{css,js,images}/* resources without allowing http://host/resources/* itself, , http://host/{controller}/{action}/{id}?{query} src/url_mapping.php file denying direct access http://host/src.
for now, closest managed following .htaccess file:
<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> in case first intention was: if users tries /resources or /src folder filtered src/url_mapping.php well, although trying several combinations no success on , on again.
i noted mod_rewrite in loop such way can not block resources directory src/url_mapping.php, limited .htaccess file hands tied, if there way accept solution.
the last choice 1 redirect , treating files in resources src/url_mapping.php, kill cache, avoiding reinvent wheel.
well, realised apache not smart, acceptable solution found changing directory tree hide non-resources content such way apache cannot access.
imagine documentroot /home/user/www, if browse http://hostname/somefile point /home/user/www/somefile, way files in /home/user/somedir/somefile not accessible apache, accessible php script if has ownership/permission on directory.
so, in resources put on /home/user/www , src on /home/user/private_html, url_mapping.php stayed on /home/user/www/index.php, way can fool user if throw 404 not found when direct accessing it.
the final tree:
project_root |- private_html | |- model | |- view | |- controller | -- ...others -- public_html |- css |- js |- images -- index.php where .htaccess file is:
<ifmodule mod_rewrite.c> rewriteengine on rewritebase / # change if there subdirectory rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewritecond %{request_filename} !-l rewriterule ^(.*)$ index.php/$1 [l] </ifmodule> and index.php is:
<?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); ?> this way permits access index.php if directoryindex, mean, http://hostname/ allowed http://hostname/index.php 404 not found because file should not exist, http://hostname/index.php/anything , http://hostname/index.php?anything, although http://hostname/anything redirect http://hostname/index.php/anything redirect_url allowed.
Comments
Post a Comment