Notes:
- I'm using Ubuntu. So, eventual paths below follow Ubuntu's filesystem rules.
- I'm using
Apache/2.4.41 (Ubuntu)
web server. So, any settings below follow its rules.
Disallow access to the whole filesystem:
First of all, you need to assure, that, by default, your web server (I suppose it's Apache) doesn't allow access to any resource of the filesystem. So, in the configuration file of Apache (/path/to/apache2/apache2.conf
) should appear the following:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
The project root and the document root shouldn't be the same:
Right now, your document root is the same with the project root. Though, they shouldn't be the same.
First, the whole project root should be made inaccessible to public access. Disallowing the access to the whole filesystem by default - as described above - should achieve this.
Second, in the project root you should create a new directory - let's name it public
- and use it as the document root. Only this folder of your MVC-based application should be publicly accessible! Therefore, the file index.php
should be moved to it. To this directory belong all other publicly accessible assets too: .css files, .js files, images, node_modules
, fonts, etc.
Create a virtual host for your application:
In the configuration file of your web server (/path/to/apache2/apache2.conf
) you should further create a virtual host, as below. Alternatively, these virtual host settings can be written in a .htaccess
file (/path/to/projects/myMVC/.htaccess
). The file should be created in the project root.
<VirtualHost *:80>
# The server name is completed in the address bar of the
# browser, in order to access the MVC-based application.
# The name is your choice.
ServerName local.my-mvc
DocumentRoot "/path/to/projects/myMVC/public"
<Directory "/path/to/projects/myMVC/public">
# Access is allowed unconditionally...
Require all granted
# ... or can be limited to a certain ip (here, as example: localhost, e.g. 127.0.0.1)
# Require ip 127.0.0.1
# When Options is set to "off", then the RewriteRule directive is forbidden!
Options FollowSymLinks
# Activate rewriting engine.
RewriteEngine On
# Use RewriteBase if webserver's URL is not directly related to physical file paths.
RewriteBase /
# Rewrite the request if the requested path and file doesn't directly match a physical file.
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the request if the requested path and file doesn't directly match a physical folder.
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite the request to the index.php script.
RewriteRule ^(.*)$ index.php [QSA,L]
</Directory>
</VirtualHost>
Add the server name of the application to the "hosts" file:
At last, the server name of the application must be added to the hosts
file (/etc/hosts
), as follows. Note that you'll need to have administrator rights in order to save the changes made to this file:
127.0.0.1 local.my-mvc
After you make sure that all of the above works as expected, you will have to edit the file index.php
with the following steps:
- Read and parse the URL, e.g. split it in components.
- Find the controller class corresponding to the controller component of the URL.
- In the found controller class find the controller method corresponding to the method component of the URL.
- Call the method.
Recommendations:
Good luck!
Resources:
About MVC-based applications:
About Apache settings: