Laravel 5 - Remove public from URL

Asked

Viewed 15,493 times

8

I developed a form and need to "throw it" in production server. I have no server access (Linux, Slackware). I access my application from the url http://ip_do_server/meus_projects/questionario/public. With the script below I get only the message Sorry, the page you are looking for could not be found. NotFoundHttpException in RouteCollection.php line 145:. I would like to know how I can get the page to load without using the /public and also know if I need to restructure his pages to make it a little safer.

This . htaccess I left at the root of my project

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

This is inside public/

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
  • Related: http://answall.com/questions/43685/problema-com-subpastas-e-reescrita-de-url-laravel

1 answer

7


There are some ways to deploy an application made with Laravel. In order of recommendation, follow the options.

The deployment options below are for the Laravel 5. I don’t know if they work in other versions.


Setting Documentroot to the /public folder

This is the way recommended by the Laravel team and the safest of all. No need to change files .htaccess, just use the default Laravel and ready. The only thing to be done is to configure your Apache. In Virtualhosts make point the DocumentRoot to the folder public as follows:

<VirtualHost *:80>
   ServerAdmin [email protected]
   DocumentRoot "/path/to/app/public"

   <Directory "/path/to/app/public">
       Options Indexes FollowSymLinks Includes ExecCGI
       AllowOverride All
       Require all granted
   </Directory>
</VirtualHost>

Using alias and . htaccess

A way to deploy without touching the DocumentRoot is using Apache aliases and setting a .htaccess inside the public folder a little different from the .htaccess pattern. Using Alias, your Virtualhost would look like this below:

<VirtualHost *:80>
   ServerAdmin [email protected]
   DocumentRoot "/path/to/apps"

   Alias /app1 "/path/to/apps/app1/public"
   <Directory "/path/to/apps/app1/public">
       Options Indexes FollowSymLinks Includes ExecCGI
       AllowOverride All
       Require all granted
   </Directory>

   Alias /app2 "/path/to/apps/app1/public"
   <Directory "/path/to/apps/app1/public>
       Options Indexes FollowSymLinks Includes ExecCGI
       AllowOverride All
       Require all granted
   </Directory>
</VirtualHost>

This way you can put multiple applications in the same DocumentRoot and separated by directories. In each application, in the directory public, would need to set the .htaccess as follows:

Application 1:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /app1

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Application 2:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /app2

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Your applications would respond in URLS http://www.example.com/app1 and http://www.example.com/app2. Take a good look at the .htaccess on the line RewriteBase /app2. This is to fix a paging problem of Laravel itself.


Moving the files from the public folder

This is the least recommended way, because it opens security loopholes. Meanwhile, let’s go to it. Use at your own risk.

  1. Copy all contents of the public folder to the root of your project.
  2. Open the file index.php at the root of the project and make the following modifications

Of:

require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';

To:

require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/start.php';

This solution will allow you to request any file at its root, including the file .env. Try to access (http://example.com/app/.env) and you will see your database connection information there!

You will have to protect all files that you do not want direct request using the .htaccess. I didn’t test it, but I believe that way:

RewriteRule \.(.env)$ - [F]

Once again, do not recommend this solution. Try to contact host and ask to point the Document Root from the URL to the public folder. Use at your own risk.

  • Having access to the server, only using virtualhost the way you said it already has more security ? There is no safer way ?

  • Security is something relative, because even applying the first solution does not guarantee 100% security. But comparatively, the first solution is more secure than all the others. The Laravel itself was designed to run the first way. The others are "hacks" on top of the framework. There is no alternative solution to the first solution that the Laravel team offers. #sadbuttrue

  • I know that some Hosts offer the possibility to point out the folder you want as Documentroot when you create a new subdomain. This is a good alternative. You can create the http://app.example.com subdomain and point the Document Root for that subdomain to the public folder,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.