php - Emulating Node/Express Routes in .htaccess -
i have been trying emulate how nodejs/express work routes. forwarding traffic index.php
process routes (using altorouter).
my file struture this:
-/ --public/ |- assets |- ... --routes/ |- route.php |- ... --index.php
take these urls instance (all should return/redirect 404):
http://testsite.com/routes
http://testsite.com/routes/route.php
http://testsite.com/somefile.php
however assets should directly accessible (i dont want include /public/
:
http://testsite.com/assets/image.png
http://testsite.com/assets/styles/style.css
this have far:
# make sure mod_rewrite on <ifmodule mod_rewrite.c> rewriteengine on # should allow our assets , keep them public rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule ^(.*)\.(gif|jpg|png|jpeg|css|js|swf)$ /public/$1.$2 [l,nc] # forward other traffic index.php rewriterule ^.+$ index.php [l] </ifmodule>
one issue i've come accross going: http://testsite.com/routes
produces:
i guess main question isnt in public shouldnt accessable (not sure if .htacces way go or not)
you don't have bury files above web root if use right rules. private files can made inaccessible.
<ifmodule mod_rewrite.c> rewriteengine on # transform assets urls correct path, , proceed next rule # checking existence in rewriteconds useless here since public urls not 1:1 physical paths rewriterule ^assets/.+ public/$0 [ns,dpi] # send *all* urls index.php except point asset file exists rewritecond $1 !=public/assets/ [or] # change slash in next condition sub-directory if not serving web root, e.g. %{document_root}/project/root/$0 rewritecond %{document_root}/$0 !-f rewriterule ^((?:[^/]+/){2})?.+ index.php [ns,end] </ifmodule>
Comments
Post a Comment