davy_yg's avatar
Level 27

Removing public folder in laravel local

Hello,

Someone mentions about removing the public folder from laravel localhost url. I only wonder if there is any benefits in it? First, I normally use the public folder as a reference from which folder I need to copy paste into the server public_html folder. Is removing the public folder from laravel localhost is a common practice?

ref:

0 likes
7 replies
jlrdw's avatar

You don't remove the public folder, you point to the public folder as document root. It means it's not in url.

I would suggest following the laravel documentation, and setup laravel correctly.

davy_yg's avatar
Level 27

Okay, I try following this article to erase the public folder on local: https://hdtuto.com/article/laravel-remove-public-from-url-using-htaccess

I can run the file afterwards without public but there are many missing images link why is it?

Also, my other question: if I set my laravel folder to remove the public url then, if I copy paste my whole laravel folder to shared hosting exactly the same way: will it works in the shared hosting?

Snapey's avatar

@davy_yg articles like this are terrible advice. Unfortunately there is no way to have them taken down.

The correct solution is to not change the laravel folder structure, but make sure your host serves the public folder as the document root (as you have been advised by @jlrdw)

jlrdw's avatar

@davy_yg no matter if using nginx or apache, you point to public as the document root. I generally don't use these third party tutorials from the web, I usually follow the documentation.

If you cannot set a virtual host, there are ways of point to public with two .htaccess files. Top level one points to public as document root, the one in public is left as is out of box.

Either way, you don't need all those other web tutorials, just point to public as document root.

Just suggestions.

Also I have had no problems with images or css when laravel is properly setup.

davy_yg's avatar
Level 27

What do you mean by: point to public as document root ?

Do you mean placing the .htaccess file in public folder as usual ?

Do I really need to erase "public" from the url ?

I also cannot find how to do this in laravel document.

jlrdw's avatar

@davy_yg you have to refer to the Apache documentation for properly setting up a virtual host. Laravel has an nginx example in the documentation.

@davy_yg just a suggestion to setup correctly as per documentation.

But haven't you done the setups in the past?

Edit:

I do wish @taylorotwell had apache example in documentation, as this question comes up often.

See this and look at @snapey answer: https://laracasts.com/discuss/channels/laravel/remove-publicindexphp-from-url-in-laravel

GregorD's avatar

I had no end of problems setting up my local development environment. I have a Windows 10 client and have Apache on a CentOS 7 computer. To expand upon what jlrdw said, I will explain my set up. Of course, it is likely to be different for you, but might give you some guidance.

At the end of my Apache configuration file (httpd.conf), I have an entry:

#####
#  Optional Includes here for Virtual Hosts
####
IncludeOptional /etc/httpd/conf/vhosts/*.conf

There is a vhosts subfolder in the folder that has httpd.conf and, in that, I have separate files, one for each laravel site I am running. I name them according to the site and the port I use to access it, such as mysite_8090.conf. This file contains:

# IPv4 only
Listen 0.0.0.0:8090

<VirtualHost *:8090>

  DocumentRoot "/full/path/to/mysite/public"
  ServerName mysite.test

  <Directory "/full/path/to/mysite/public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>

</VirtualHost>

If I add another Laravel site, I copy mysite_8090.conf and rename it, changing the contents to reflect a different port, DocumentRoot and Directory. Don't forget to restart your httpd if you add another config file.

Finally, in my Windows 10 client, I update my hosts file, adding an entry which points mysite.test (the ServerName in my config file) to the (static) IP address of my CentOS 7 computer. In order to access my Laravel site, I use http://mysite.test:8090 and it goes to the usual landing page showing the familiar Laravel information. There is no sign of /public/ in the URI.

As I said, this is to give guidance and indicate what works for me. There are several examples for setting up Apache VirtualHosts here: https://httpd.apache.org/docs/2.4/vhosts/examples.html. I don't have any experience of Nginx but suspect it will have a similar configuration. I think they are called Server Blocks, rather than VirtualHosts.

Please or to participate in this conversation.