Marcolino922's avatar

Move content from "public" to the root?

Hi, I know this is a really stupid question but I always develop my applications by moving the contents of the "Public" folder into the root, obviously putting everything in safety. I got used to it, but I understand that it is not the correct way.

I would like to understand better how to access my new Laravel installation as if there was already the index in the root instead of going into the public folder.

Let me explain even better, I would like to understand how to access my site example.com instead of example.com/public

Do I need to edit the .htaccess file? And inserting what? I ask for patience, unfortunately I got used to that other way and I would like to redeem myself :)

0 likes
13 replies
Snapey's avatar

webservers need to be told what folder they are serving the content from. This is called the document root

You just configure your webserver to serve the public folder

That's all there is to it

Marcolino922's avatar

Can I then rename the file server.php to index.php and copy the .htaccess file to the root?

Snapey's avatar

no, why?

The framework comes as you should deploy it. no need to be moving things around or changing paths inside index.php

jlrdw's avatar

Can I then rename the file server.php to index.php and copy the .htaccess file to the root?

I would advise not doing that, setup laravel correctly.

Two ways:

  • As mentioned point to public as document root.

Or

  • Have main laravel out of web, offers highest security.

If you are not sure how to accomplish option 2, then use option 1.

Marcolino922's avatar

Unfortunately I am not able to solve, I state that I am using XAMPP, I did it this way, once I have saved the file I restart xampp, but now only the 404 error appears by going to localhost/project

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/project/public"
    ServerName test.dev
    ServerAlias test.dev
    ErrorLog "logs/laravel.log"
    CustomLog "logs/custom.laravel.log" combined
    <Directory "C:/xampp/htdocs/project/public">
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>
Snapey's avatar

your site will be available at test.dev

It won't be available at https://localhost because you need to present the name of the site in your browser request in order for virtual hosts to work.

You may need to add an entry to your hosts file so that test.dev resolves to localhost

Marcolino922's avatar

Back to the discussion, by necessity I would need to simplify this process and find a solution such as editing the htaccess. In this regard, in the root I created the .htacces file by inserting the following code but also this time 404 error appears.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>
Snapey's avatar
  1. Setup virtual host in apache with the document root pointing to the project's public folder

  2. use the htaccess file that ships with Laravel

  3. create a hosts entry that points your chosen domain name to your local machine.

Marcolino922's avatar

I would like to find a solution where my possible client who goes to install my application does not have to make these changes, but after having uploaded all the files to be able to visit his site already as mysite.com. What solutions can I adopt? I've tried them all.

Snapey's avatar

laravel comes ready out of the box to install on any web hosting, but you do need to know what you are doing according to the hosting type

Unless you can understand the concepts of the document root and virtual hosting then you are not going to be able to explain it to anyone else.

Please or to participate in this conversation.