V
V
Vasily Andreev2015-11-29 20:55:26
PHP
Vasily Andreev, 2015-11-29 20:55:26

How to correctly move the site root directory to a public subdirectory?

Hello.
All of these newfangled frameworks and "how to do it right" articles (like Create your own PHP Framework ) recommend wrapping all requests to the front controller first, and then moving it to a separate folder (like web or public ). All static files (such as images or style files) are also stored there. For security reasons, allow clients to access only this public folder and deny everything else.
It's just not clear how to do it right. I had an option to create .htaccess in the root of the site

RewriteEngine on
RewriteRule ^web/(.*)$ web/$1 [L]
RewriteRule ^(.*)$ web/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ web/index.php

Only I had some doubts. Maybe it's better to ask the hoster to rewrite VirtualHost and change the root directory of the site right in the Apache config?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2015-11-30
@PyroRed

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

And in the public folder, one more needs to be done
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question