6
I’m thinking of creating a very limited php microframework just for own use, however I came across a situation, most frameworks uses a folder called public
and on the generally production servers we point this folder with DocumentRoot
through the httpd.conf
.
The structures are usually like this:
/home/user/projeto
├── data/
├── vendor/
├── application/
└── public/
├── .htaccess
└── index.php
The vhost is similar to this:
<VirtualHost *:80>
ServerName myapp.localhost.com
DocumentRoot "/home/user/projeto/public"
<Directory "/home/user/projeto/public">
AllowOverride all
</Directory>
</VirtualHost>
But as in my case it’s a microframework simple for personal use I thought of using so the folder structure:
/home/user/projeto
├── index.php
├── .htaccess
└── exemplo/
├── application/
├── vendor/
└── data/
In this second example the /home/user/projeto/.htaccess
this way:
IndexIgnore *
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule "^exemplo/" "index.php" [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Note that I used RewriteRule "^exemplo/" "index.php" [L]
to prevent accessing the contents of the folder exemplo/
where you can find the project files, libraries and classes and the folder data/
which is used to save non-public data. Also if accessed like this: http://site/exemplo/
he will execute the /home/user/projeto/index.php
.
The reason you want to use the second way is to be able to easily port the application to other servers without having to configure the DocumentRoot
and restart Apache, making settings easier.
The question is:
- Use
RewriteRule "^exemplo/" "index.php" [L]
to prevent access to the folderexemplo/
where I keep the foldersdata/
,vendor/
andapplication/
may be unsafe or may cause some other kind of problem?
I stopped answering because I got a little confused, and although there are no security problems visually, I feel like something is slipping away, because the way it is, the user is practically in the directory
exemplo
you understand ? He basically already has access to this directory simply because it is in the public directory, the only thing that separates him from the rest is the redirect that is triggered if it is accessed.– Edilson
It wouldn’t be the same as putting multiple files
index.php
withheader(location)
in the directoryexemplo
and those that follow ?– Edilson
Look, disable all the
.htaccess
and creates a fileindex,php
in the briefcaseexemplo
withheader("location:../"); exit();
, and forehead.– Edilson
@Edilson I know how to use
header("location:../"); exit();
orheader("", true, 403); exit();
works to override access to the example. But please understand that theRewriteRule
already overrides access. The question is whether to use theRewriteRule "^exemplo/" "index.php" [L]
has some security problem, you understand?– Guilherme Nascimento
It wouldn’t be the same, but it’s pretty much the same, that’s what I wanted to prove. Although it may seem convenient, I think it would be good if you kept the first form, although it seems more laborious. Still I’m vague the idea.
– Edilson
@Edilson would not be the same, because Location redirects in the browser, rewriterule is an internal redirect, that is, a rewritten url. Yes is the point I want to make, why keep the first form that is more laborious? Maybe this answers the question :)
– Guilherme Nascimento
@Guilhermenascimento does not apply to your case, because it is to run in several different places, but only of curiosity: I have a system where the Documentroot is PHP and not the folder. So all the way is managed by PHP, and neither . htaccess uses. Only that there is nothing else static on the site. header("X-Sendfile: $filename"); helps not to overload PHP.
– Bacco