Laravel 5 – Remove Public from URL

后端 未结 30 1924
甜味超标
甜味超标 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:49

    You can remove public keyword from url using various methods.

    1) If you are using dedicated hosting and you have root access then You can remove public keyword from url using Virtual Host. You should give DocumentRoot path with public. So this will start index from public directory and remove it from url.

    <VirtualHost *:80>
        ServerAdmin info@example.com
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/html/{yoursourcedirectory}/public
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    2) If you dont have root access of your hosting then you should genarate a new .htaccess file in your root directory and put the code as below

    <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]
    
    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php
    </IfModule>
    

    You can get more reference here.

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

    BEST Approach: I will not recommend removing public, instead on local computer create a virtual host point to public directory and on remote hosting change public to public_html and point your domain to this directory. Reason, your whole laravel code will be secure because its one level down to your public directory :)

    METHOD 1:

    I just rename server.php to index.php and it works

    METHOD 2:

    This work well for all laravel version...

    Here is my Directory Structure,

    /laravel/
    ... app
    ... bootstrap
    ... public
    ... etc
    

    Follow these easy steps

    1. move all files from public directory to root /laravel/
    2. now, no need of public directory, so optionally you can remove it now
    3. now open index.php and make following replacements

    require DIR.'/../bootstrap/autoload.php';

    to

    require DIR.'/bootstrap/autoload.php';

    and

    $app = require_once DIR.'/../bootstrap/start.php';

    to

    $app = require_once DIR.'/bootstrap/start.php';

    1. now open bootstrap/paths.php and change public directory path:

    'public' => DIR.'/../public',

    to

    'public' => DIR.'/..',

    and that's it, now try http:// localhost/laravel/

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

    Create .htaccess file in root directory and place code something like below.

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule ^ ^$1 [N]
    
        RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
        RewriteRule ^(.*)$ public/$1 
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ server.php
    </IfModule>
    
    0 讨论(0)
  • 2020-11-22 03:52

    1) I haven't found a working method for moving the public directory in L5. While you can modify some things in the bootstrap index.php, it appears several helper functions are based on the assumption of that public directory being there. In all honestly you really shouldn't be moving the public directory.

    2) If your using MAMP then you should be creating new vhosts for each project, each serving that projects public directory. Once created you access each project by your defined server name like this :

    http://project1.dev
    http://project2.dev
    
    0 讨论(0)
  • 2020-11-22 03:52

    Create A .htaccess file in your root DIR and paste the below code. That's it :P

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]
    
    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

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

    Let's say you placed all the other files and directories in a folder named 'locale'.

    enter image description here

    Just go to index.php and find these two lines:

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

    and change them to this:

    require __DIR__.'/locale/bootstrap/autoload.php';
    
    $app = require_once __DIR__.'/locale/bootstrap/app.php';
    
    0 讨论(0)
提交回复
热议问题