Laravel 5 - Remove public from URL

7

I've developed a form and need to "play" it on the production server . I do not have any type of server access (Linux, Slackware). I access my application through the link url. 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 loaded without using /public and also know if I need to restructure its pages to be a little more secure.

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 within 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>
    
asked by anonymous 10.04.2015 / 16:07

1 answer

7

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

The deploy options below are for Laravel 5 . I do not know if they work in other versions.

Setting the DocumentRoot to the / public folder

This is the form recommended by the Laravel team and the safest of all. There is no need to change .htaccess files, just use the Laravel pattern and you're done. The only thing to do is configure your Apache. In VirtualHosts, point the DocumentRoot to the public folder 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 aliases and .htaccess

One way to deploy without touching DocumentRoot is by using Apache aliases and setting a. htaccess within the public folder slightly different from the% default%. 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 .htaccess and separated by directories. For each application, in the DocumentRoot directory, you would need to set public 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 apps would respond to the URLs link and link . Look closely at .htaccess on line .htaccess . This is to correct a paging problem for Laravel himself.

Moving the files from the public folder

This is the least recommended way , because it opens up security holes. However, let us go to it. Use at your own risk.

  • Copy the entire contents of the public folder to the root of your project.
  • Open the RewriteBase /app2 file in the project root and make the following modifications
  • From:

    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 index.php . Try accessing ( link ) and you'll see your database connection information there!

    You will have to protect all files that you do not want to request directly using .env . I have not tested, but I believe that this way:

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

    Again, I do not recommend this solution. Try to get in touch with being a host and ask to point the Document Root of the URL to the public folder. Use at your own risk.

        
    10.04.2015 / 21:47