lundi 1 février 2016

Precautions after Removing 'public' from URL in Laravel 5

I tried to find a solution to remove the 'public' from the url in a laravel 5 application. (localhost/laravelapp/public)

This SO answer was very useful but since i have used the asset helper, css, js and custom folder for uploading documents couldn't be found. So i had to try different answers to find the one that worked for me. While some answers suggest it's not good to remove the public folder at all, other suggest it's better to rename it instead of removing it.

I'm just wondering if there are any dangers or if there is anything i missed while removing the 'public' folder as per the answer i found in SO. Here is what i did

Just like KA_lin's suggestion

Renamed the server.php to index.php (no modifications)

Copied the .htaccess from public to root

Changed .htaccess to the following

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

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]

</IfModule>

Now, the css and js files were not loading properly and as per Sean Perkins answer, i opened vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php and changed

public function asset($path, $secure = null)
{
    if ($this->isValidUrl($path)) {
        return $path;
    }

    $root = $this->getRootUrl($this->getScheme($secure));
    return $this->removeIndex($root).'/'.trim($path, '/');
}

to

public function asset($path, $secure = null)
{
    if ($this->isValidUrl($path)) {
        return $path;
    }

    $root = $this->getRootUrl($this->getScheme($secure));
    return $this->removeIndex($root).'/public/'.trim($path, '/');
}

Now everything seems to work fine.

After that, i also added the following line in .htaccess since the .env file was being viewed when i requested localhost/laravelapp/.env

RewriteRule ^.env - [F,L,NC] 

Now everything seems ready but i was wondering if there are any other precautions i should take before i upload the website to a live server.

i also noticed that the website will load all the content without css or js when i navigate to localhost/laravelapp/public. Should i be worried about it.

I also didn't delete the .htaccess inside the public folder. I'm assuming its not a problem since i have a new one inside the root folder. Is it ok to delete it.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire