首页 文章

Laravel 5 - 从URL中删除public

提问于
浏览
172

我知道这是一个非常受欢迎的问题,但我无法为Laravel 5找到一个可行的解决方案 . 我一直试图从Codeigniter迁移很长一段时间,但这个复杂的安装过程一直让我失望 .

我不想运行VM,这在项目之间切换时似乎很尴尬 .

我不想将我的文档根目录设置为公共文件夹,这在项目之间切换时也很尴尬 .

我试过了.htaccess mod_rewrite方法

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

这只是在compiled.php第7610行中给了我一个Laravel NotFoundHttpException .

当我不久前尝试L4时,我使用了将公用文件夹的内容移动到根目录的方法 . L5的结构完全不同,按照相同的步骤完全打破了Laravel(服务器只会返回一个空白页面) .

是否有一种很好的方法可以在开发环境中删除“公共”:

  • 适用于L5

  • 允许我轻松切换项目(我通常在任何时候工作2或3) .

谢谢

**我正在使用MAMP和PHP 5.6.2

23 回答

  • 41

    在laravel5中可以从url中删除public . 跟着这些步骤:

    步骤1从public复制所有文件并粘贴到根目录

    步骤2打开index.php文件替换为

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

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

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

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

    并删除所有缓存和cookie .

  • 18

    从laravel 5 url中删除公众的简便方法 . 你只需要从公共目录中剪切index.php和.htaccess并将其粘贴到根目录中,这就是所有并将index.php中的两行替换为

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

    在Laravel 5.5中,在根目录中创建.htacess文件并放置以下代码: - Reference Link

    <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>
    
  • 350

    WARNING: 我不建议删除public,而是 on local computer create a virtual host point to public directoryon remote hosting change public to public_html and point your domain to this directory . 原因,你的整个laravel代码将是安全的,因为它的一级到你的公共目录:)

    方法1:

    我只是将 server.php 重命名为 index.php 并且它有效

    方法2:

    这适用于所有laravel版本......

    这是我的目录结构,

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

    按照这些简单的步骤

    • 全部移动 files from public directory to root /laravel/

    • 现在,不需要公共目录,因此您可以选择立即删除它

    • 现在打开index.php并进行以下替换

    要求DIR . '/ .. / bootstrap / autoload.php';

    要求DIR . '/ bootstrap / autoload.php';

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

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

    • 现在打开bootstrap / paths.php并更改公共目录路径:

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

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

    就是这样,现在尝试http:// localhost / laravel /

  • 12

    对于Laravel 5:

    • 将Laravel根文件夹中的 server.php 重命名为 index.php

    • .htaccess 文件从 /public 目录复制到Laravel根文件夹 .

    而已 !! :)

  • 1

    @ rimon.ekjon说:

    将Laravel根文件夹中的server.php重命名为index.php,并将.htaccess文件从/ public目录复制到Laravel根文件夹 . - 而已 !! :)

    这对我有用 . 但是/ public目录中的所有资源文件都找不到并且请求url不起作用,因为我使用了asset()帮助程序 .

    我更改了/Illuminate/Foundation/helpers.php/asset()函数如下:

    function asset($path, $secure = null)
    {
        return app('url')->asset("public/".$path, $secure);
    }
    

    现在一切正常:)

    谢谢@ rimon.ekjon和你们所有人 .

  • 3

    1)我还没有找到一种在L5中移动公共目录的工作方法 . 虽然你可以修改bootstrap index.php中的一些东西,但是看起来有几个辅助函数是基于那个公共目录的假设 . 老实说,你真的不应该移动公共目录 .

    2)如果您使用MAMP,那么您应该为每个项目创建新的vhost,每个项目都是项目公共目录 . 创建后,您可以按定义的服务器名称访问每个项目 . http://project1.devhttp://project2.dev

  • 34

    即使我不建议将Laravel放在根文件夹上,也有一些无法避免的情况;对于这些情况,上述方法不适用于资产,所以我快速修改了htaccess:将server.php复制到index.php之后编辑.htaccess文件,如下所示:

    <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>
    

    这是我必须做的快速修复,所以欢迎任何人升级它 .

  • 22

    即使这个帖子中提到的解决方案适用于你们,我也会要求你不要这样做 . 这就是原因所在 .

    您应该将Apache主机根目录指向 $LARAVEL_PATH/public 目录而不是 $LARAVEL_PATH .

    拥有www主机根目录而不是项目根目录的重点是,您不会通过Web服务器泄露任何项目文件 .

    即使所有PHP文件都有文件后缀 .php ,恶意用户也可以访问 $LARAVEL_PATH/storage 目录及其子目录内容,读取 composer.jsonpackage.json 以查找易受攻击的依赖项或读取 .env 文件等 .

    如果您在共享主机上运行并且必须 public_html ,请尝试在 public_html 目录之外安装Laravel并删除public_html(如果为空)并将其替换为符号链接到 $LARAVEL_PATH/public 或者如果您希望Laravel实例是 public_html 的子目录,做同样的事情但是从 $LARAVEL_PATH/publicpublic_html/$PROJECT_SUBDIR 创建符号链接 .

    该公共目录是有理由使项目更安全 . 解决实际问题,不要试图打破这个简单但很好的安全补充 . :)

  • 1

    只需在root创建 .htaccess 文件并将这些行添加到它

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
    

    而已!

    上面的代码适用于root的public_html文件夹 . 说过你的核心laravel文件应该在public_html文件夹中,如果你的目录看起来像public_html / laravelapp / public,如果把上面的代码放在laravelapp中,它将无法工作 . 因此,您必须将所有核心文件复制到public_html中并将.htaccess文件放在那里 .

    如果您想将代码保存在子目录中,那么您可以创建一个子域,那么此代码也适用于此 .

  • 11

    我之前看过一些文章并且它工作正常,但真的不知道是否安全

    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.
    

    编辑index.php

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

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

    首先,您可以使用此步骤

    对于Laravel 5:1 . 将Laravel根文件夹中的server.php重命名为index.php 2.将.htaccess文件从/ public目录复制到Laravel根文件夹 .

    来源:https://stackoverflow.com/a/28735930

    按照这些步骤后,您需要更改所有的CSS和脚本路径,但这将是累人的 .

    Solution Proposal :只需稍微更改 helpers::asset 功能即可 .

    For this

    • 打开 vendor\laravel\framework\src\Illuminate\Foundation\helpers.php

    • 转到第130行

    • "public/".$path 而不是 $path

    function asset($path, $secure = null){
       return app('url')->asset("public/".$path, $secure);
    }
    
  • 100
    You can follow only 2 step
    
    
     1- cut .htaccess from public folder and paste to root(mainFolder)  
     2- Rename server.php file to index.php AT your root dir. (mainFolder)
    
  • 36

    Laravel 5.5

    在第一次安装Laravel之后,我遇到了着名的"public folder problem",我想出了这个解决方案,在我的 personal opinion 中,是"cleaner",然后是我在网上找到的其他人 .


    成就

    • URI中没有 public

    • 保护 .env 文件免受好奇的人的伤害

    只需使用 mod_rewrite 和四个简单规则编辑 .htaccess 即可完成所有操作 .


    步骤

    • 在主根中移动 .htaccess 中的 .htaccess 文件

    • 编辑如下

    我评论了一切,所以对于那些从未使用 mod_rewrite (不是我是专家,反之亦然)的人也应该清楚(我希望) . 另外,为了理解这些规则,必须清楚的是,在Laravel中,如果Bill连接到 https://example.com ,则会加载 https://example.com/index.php . 该文件只包含命令 header("refresh: 5; https://example.com/public/") ,它将请求发送到 https://example.com/public/index.php . 第二个 index.php 负责加载控制器和其他东西 .

    # IfModule prevents the server error if the app is moved in an environment which doesn’t support mod_rewrite
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # RULES ORIGINALLY IN public/.htaccess ---
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]
    
        # Handle Front Controller...
    #    RewriteCond %{REQUEST_FILENAME} !-d
    #    RewriteCond %{REQUEST_FILENAME} !-f
    #    RewriteRule ^ index.php [L]
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        # --- END
    
        # PERSONAL RULES ---
        # All the requests on port 80 are redirected on HTTPS
        RewriteCond %{SERVER_PORT} ^80$
        RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
    
        # When .env file is requested, server redirects to 404
        RewriteRule ^\.env$ - [R=404,L,NC]
    
        # If the REQUEST_URI is empty (means: http://example.com), it loads /public/index.php
        # N.B.: REQUEST_URI is *never* actually empty, it contains a slash that must be set as match as below
        # .* means: anything can go here at least 0 times (= accepts any sequence of characters, including an empty string)
        RewriteCond %{REQUEST_URI} ^/$
        RewriteRule ^(.*) /public/index.php [L]
    
        # If the current request is asking for a REQUEST_FILENAME that:
        # a) !== existent directory
        # b) !== existent file
        # => if URI !== css||js||images/whatever => server loads /public/index.php, which is responsible to load the app and the related controller
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule !^(css|js|images|media)/(.*)$ /public/index.php [L,NC]
    
        # If the current request is asking for a REQUEST_FILENAME that:
        # a) !== existent directory
        # b) !== existent file
        # => if URI == css||js||images[=$1]/whatever[=$2] => server loads the resource at public/$1/$2
        # If R flag is added, the server not only loads the resource at public/$1/$2 but redirects to it
        # e.g.: bamboo.jpg resides in example.com/public/media/bamboo.jpg
        #       Client asks for example.com/media/bamboo.jpg
        #       Without R flag: the URI remains example.com/media/bamboo.jpg and loads the image
        #       With R flag: the server redirects the client to example.com/public/media/bamboo.jpg and loads the image
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(css|js|images|media)/(.*)$ /public/$1/$2 [L,NC]
        # --- END
    
    </IfModule>
    

    可以删除以下规则(最初在 public/.htaccess 中) . 事实上,同样的规则在最后两条规则中以更详细的方式阐明 .

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

    EDIT: 我错过了Abhinav Saraswat's solution,他的回答应该是被接受的 . 只有一个简单明了的规则,可以将所有流量重定向到公用文件夹,而无需修改任何文件 .

  • 7

    我用2个答案解决了这个问题:

    • 将server.php重命名为index.php(无修改)

    • 将.htaccess从公共文件夹复制到根文件夹(如下面的rimon.ekjon所述)

    • 更改.htaccess它有点如下静态:

    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]
    

    如果还需要其他 static files ,只需将扩展名添加到上一个声明的列表中

  • 4

    我使用的另一种方法是使用htdocs或www中的ln命令创建一个符号链接(在Linux中,不知道Win),即 ln projectname/public project 因此可以通过localhost / project访问该站点

  • 0

    我想补充一下@Humble Learner并注意到“修复”资产的url路径的正确位置是/Illuminate/Routing/UrlGenerator.php/asset() .

    更新方法以匹配:

    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, '/');
    }
    

    这将修复脚本,样式和图像路径 . 任何资产路径 .

  • 7

    根目录中的 step 1: server.php文件重命名为index.php

    step 2:

    将.htaccess文件从公共目录复制到root direct

  • 4

    不要:

    You really should not 将Laravel根文件夹中的server.php重命名为index.php,并将.htaccess文件从/ public目录复制到Laravel根文件夹!

    这样每个人都可以访问您的一些文件(例如.env) . 你不希望这样!


    做:

    相反,您应该在您的根目录中创建一个.htaccess文件,如下所示:

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

    而已 .

  • 1

    假设您将所有其他文件和目录放在名为“locale”的文件夹中 .

    enter image description here

    只需转到index.php并找到以下两行:

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

    并将它们改为:

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

    总是有理由在Laravel设置中有一个公共文件夹,所有公共相关的东西都应该出现在公共文件夹中,

    不要将您的IP地址/域指向Laravel的根文件夹,而是将其指向公用文件夹 . 将服务器Ip指向根文件夹是不安全的,因为除非你在.htaccess中写入限制,一个人可以轻松访问其他文件 . ,

    只需在 .htaccess 文件中写入重写条件并安装重写模块并启用重写模块,在路由中添加公共的问题将得到解决 .

  • 8

    对于XAMPP用户在不触及laravel默认文件系统的情况下从url中删除public是为您的应用程序设置虚拟主机以执行此操作jsut请按照以下步骤操作

    • 打开XAMPP控制面板应用程序并停止Apache . 请注意,较晚的Windows计算机可能将其作为服务运行,因此请选中Apache模块左侧的框 .

    • 导航到 C:/xampp/apache/conf/extra 或XAMPP文件所在的位置 .

    • 使用文本编辑器打开名为httpd-vhosts.conf的文件 .

    • 第19行找到# NameVirtualHost *:80 并取消注释或删除哈希 .

    • 在文件的最底部粘贴以下代码:

    <VirtualHost *>
        ServerAdmin admin@localhost.com
        DocumentRoot "C:/xampp/htdocs" # change this line with your htdocs folder
        ServerName localhost
        ServerAlias localhost
        <Directory "C:/xampp/htdocs">
            Options Indexes FollowSymLinks Includes ExecCGI
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    
    • 现在,您可以复制并粘贴上面的代码以添加虚拟主机目录 . 例如,我正在一个名为Eatery Engine的网站上工作,所以下面的代码片段将允许我在本地安装上使用子域:
    <VirtualHost eateryengine.dev>
        ServerAdmin admin@localhost.com
        DocumentRoot "C:/xampp/htdocs/eateryengine" # change this line with your htdocs folder
        ServerName eateryengine.dev
        ServerAlias eateryengine.dev
        <Directory "C:/xampp/htdocs/eateryengine">
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    
    • 接下来转到Windows主机文件以编辑您的HOSTS . 该文件将位于 C:/Windows/System32/drivers/etc/hosts ,其中hosts是文件 . 用记事本打开它 .

    • 查找#localhost名称解析是在DNS本身内处理的 .

    127.0.0.1 localhost

    localhost名称解析在DNS本身内处理 .

    127.0.0.1       localhost
    127.0.0.1       eateryengine.dev #change to match your Virtual Host.
    127.0.0.1       demo.eateryengine.dev #manually add new sub-domains.
    
    • 重启Apache并测试一切 .

    原始文章可以找到here

  • 0

    这是最适合我的最佳解决方案 may, 2018 for Laravel 5.5

    1-只需将 .htaccess 文件从/ public目录剪切到根目录,并使用以下代码替换它:

    <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 ^ server.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
    
    </IfModule>
    

    2-只需保存 .htaccess 文件即可 . enjoy!

相关问题