I know this is a very popular question but I haven\'t been able to find a working solution for Laravel 5. I\'ve been trying to migrate from Codeigniter for a long time, but
4 best ways to remove public from the URL.
If you used any other trick to remove the public from the URL like changes the name of server.php to index.php and changing into the core file path. Clearly, don't do that. Then why Laravel not giving the solution like this because it's not a proper way to do that.
1) Remove public from URL using htaccess in Laravel
By adding a .htaccess file into the root, You can access the website without public
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
2) Remove the public by creating a virtual host in your local
I am giving demo here for the Window operating system. But I will try to define a step so that anyone can easily follow the step. You can also research on google for the same for the particular operating system.
Step 1: Go to C:\Windows\system32\drivers\etc\ open the "hosts" file in Administrator mode.
Step 2: Add the following code to it. Here, I am giving you a demo of projectname.local domain name demo, you can specify any as you like. Just make it constant at every place.
127.0.0.1 projectname.local
Step 3: Now go to, C:\xampp\apache\conf\extra
for xampp users and for the wamp user "C:\wamp\bin\apache\Apache2.4.4\conf\extra"
and open "httpd-vhosts.conf"
file. Now add the following code into it.
Notes: Change the Document root as per your project also add domain name as you define into the "hosts" file.
ServerAdmin projectname.local
DocumentRoot "C:/xampp/htdocs/projectdir"
ServerName projectname.local
ErrorLog "logs/projectname.local.log"
CustomLog "logs/projectname.local.log" common
Step 4: Last but the important step is to restart your Xampp or Wamp and access the url like http://projectname.local
and your Laravel will respond without public URL.
3) Remove the public by running the command in Laravel
If you are working in local then you don't need to do anything just need to run the following command from your terminal or command line tool. After that, you can access your website by provided URL by the command line.
> php artisan serve
If you are willing to run your project on particular IP then you need to run following command. If you are working on LAN then if you want to allow other people to access your website from local then you just need to check your IP address using command line by running "ipconfig" after getting your IP address run following the command.
> php artisan serve --host=192.168.0.177
If you are willing to run your project on a particular IP with particular port then you need to the following command.
> php artisan serve --host=192.168.0.177 --port=77
4) Remove the public on the hosted server or on the cpanel
After completion of the project you need to host the project on the server, then you just need to set the document root on your domain to the public folder. Check the below screenshot.
As per screenshot if you don't have any project folder into the public_html then you just need to set your document root like "public_html/public"
.
Reference taken from here