There is multiple options to remove the public from the url in laravel application.
let’s have look on multiple options of removing public and which one is more secure and reliable method to use.
1. Replacing server.php to index.php
Replacing the root file server.php to index.php is easiest way to remove public from url. but this is not safe and recommended method.
you also need to copy the public/.htaccess file to root folder of the website to working it out.
2. Creating a .htaccess file on root of laravel project
Creating a new file on root of laravel .htaccess and put this below code in the file
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
This code will rewrite all request to public folder silently and application will not expose to security risk.
3. Best and recommended method by laravel, creating virtual host to remove public from url
To enable this we need to edit the apache httpd.conf or httpd-vhost.conf file.
And enter the below code to enable virtual host for the specific application.
<VirtualHost *:80>
DocumentRoot "F:/xampp/htdocs/dev/public"
ServerAdmin admin@localhost
ServerName laravel.app
ServerAlias www.laravel.app
<Directory "F:/xampp/htdocs/laravelblog/public">
AllowOverride All
Options Indexes FollowSymLinks
Require local
# if you want access from other pc's on your local network
#Require ip 192.168.1
# Only if you want the world to see your site
#Require all granted
</Directory>
</VirtualHost>