Laravel 5 – Remove Public from URL

后端 未结 30 1921
甜味超标
甜味超标 2020-11-22 03:20

I know this is a very popular question but I haven\'t been able to find a working solution for Laravel 5. I\'ve been trying to migrate from Codeigniter for a long time, but

相关标签:
30条回答
  • 2020-11-22 03:35

    Even if i do not recommend putting Laravel on the root folder there are some cases when it could not be avoided; for those cases the above methods do not work for assets so i made a quick fix changing the htaccess: after copying server.php to index.php edit the .htaccess file like so:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        ### fix file rewrites on root path ###
        #select file url
        RewriteCond %{REQUEST_URI} ^(.*)$
        #if file exists in /public/<filename>
        RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
        #redirect to /public/<filename>
        RewriteRule ^(.*)$ public/$1 [L]
        ###############
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Handle Front Controller...
    
        #RewriteCond %{REQUEST_FILENAME} -d # comment this rules or the user will read non-public file and folders!
        #RewriteCond %{REQUEST_FILENAME} -f #
        RewriteRule ^ index.php [L]
    </IfModule>
    

    This was a quick fix i had to make so anyone is welcome to upgrade it.

    0 讨论(0)
  • 2020-11-22 03:35

    I have read some article before and it's working fine but really don't know is safe or not

        a. Create new folder local.
        b. Move all project into the local folder expect public folder.
        c. Move all the content of public folder to project root.
        d. Delete the blank public folder
        f. Edit the index file. 
    

    Edit the index.php

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

    to

    require __DIR__.'/local/bootstrap/autoload.php';
    $app = require_once __DIR__.'/local/bootstrap/app.php';
    
    0 讨论(0)
  • 2020-11-22 03:35

    You can simply do it in 2 easy steps

    1. Rename your server.php file in root directory of your laravel project.

    2. create a .htaccess file on root directory and paste the below code in it

    Options -MultiViews -Indexes
    
    RewriteEngine On
     
    
    **#Handle Authorization Header**
    
    RewriteCond %{HTTP:Authorization} .
    
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
     
    
    **# Redirect Trailing Slashes If Not A Folder...**
    
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteCond %{REQUEST_URI} (.+)/$
    
    RewriteRule ^ %1 [L,R=301]
    
    #Handle Front Controller...
    
    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]
    
    
    0 讨论(0)
  • 2020-11-22 03:37

    Firstly you can use this steps

    For Laravel 5:

    1. Rename server.php in your Laravel root folder to index.php

    2. Copy the .htaccess file from /public directory to your Laravel root folder.

    source: https://stackoverflow.com/a/28735930

    after you follow these steps then you need to change all css and script path, but this will be tiring.

    Solution Proposal :simply you can make minor change the helpers::asset function.

    For this:

    1. open vendor\laravel\framework\src\Illuminate\Foundation\helpers.php

    2. goto line 130

    3. write "public/".$path instead of $path,

      function asset($path, $secure = null){
         return app('url')->asset("public/".$path, $secure);
      }
      
    0 讨论(0)
  • 2020-11-22 03:38

    For Laravel 5:

    1. Rename server.php in your Laravel root folder to index.php
    2. Copy the .htaccess file from /public directory to your Laravel root folder.

    That's it!

    0 讨论(0)
  • 2020-11-22 03:38

    DON'T!

    YOU REALLY SHOULD NOT rename server.php in your Laravel root folder to index.php and copy the .htaccess file from the /public directory to your Laravel root folder!!!

    This way everyone can access some of your files (.env for example). Try it yourself. You don't want that!


    DO

    Instead, you should create an .htaccess file in your root like this:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/$1 [L,QSA]
    

    This will silently rewrite all your base URIs to the /public folder. Even all Headers, for example the HTTP Authorization Header, and all optional URI parameters will silently be passed on to the /public folder as well.

    That's all

    Please note when setting up a Laravel project with Docker: you won't need to do any of this.

    0 讨论(0)
提交回复
热议问题