Laravel 5 - 从URL中删除Public

问题描述 投票:200回答:28

我知道这是一个非常受欢迎的问题,但我无法为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(服务器只会返回一个空白页面)。

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

  1. 适用于L5
  2. 允许我轻松切换项目(我通常在任何时候工作2或3)。

谢谢

**我正在使用MAMP和PHP 5.6.2

php .htaccess laravel mamp laravel-5
28个回答
388
投票

对于Laravel 5:

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

而已!


10
投票

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

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

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

并将它们改为:

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

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

8
投票

最佳方法:我不建议删除公共,而不是require __DIR__.'/locale/bootstrap/autoload.php'; $app = require_once __DIR__.'/locale/bootstrap/app.php'; on local computer create a virtual host point to public directory。原因,你的整个laravel代码将是安全的,因为它的一级到你的公共目录:)

方法1:

我只是将on remote hosting change public to public_html and point your domain to this directory重命名为server.php并且它有效

方法2:

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

这是我的目录结构,

index.php

按照这些简单的步骤

  1. 将所有文件从公共目录移动到root / laravel /
  2. 现在,不需要公共目录,因此您可以选择立即删除它
  3. 现在打开index.php并进行以下替换

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

需要DIR。'/ bootstrap / autoload.php';

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

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

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

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

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

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


7
投票

这是对我来说最好和最短的解决方案,对于我来说,2018年5月为Laravel 5.5

  1. 只需将您的/laravel/ ... app ... bootstrap ... public ... etc 文件从/ public目录切换到根目录,并使用以下代码替换它: .htaccess
  2. 只需保存<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> 文件即可。
  3. 将您的.htaccess文件重命名为server.php。这都是享受!

6
投票

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

更新方法以匹配:

index.php

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


6
投票

在根目录中创建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, '/'); } 文件并放置如下所示的代码。

.htaccess

5
投票

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

  1. 打开XAMPP控制面板应用程序并停止Apache。请注意,后期Windows计算机可能将其作为服务运行,因此请选中Apache模块左侧的框。
  2. 导航到<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> 或XAMPP文件所在的位置。
  3. 使用文本编辑器打开名为httpd-vhosts.conf的文件。
  4. 第19行找到#C:/xampp/apache/conf/extra并取消注释或删除哈希值。
  5. 在文件的最底部粘贴以下代码:
NameVirtualHost *:80
  1. 现在,您可以复制并粘贴上面的代码以添加虚拟主机目录。例如,我正在一个名为Eatery Engine的网站上工作,所以下面的代码片段将允许我在本地安装上使用子域:
<VirtualHost *>
    ServerAdmin [email protected]
    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>
  1. 接下来转到Windows主机文件以编辑您的HOSTS。该文件将位于<VirtualHost eateryengine.dev> ServerAdmin [email protected] 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> ,其中hosts是文件。用记事本打开它。
  2. 查找#localhost名称解析是在DNS本身内处理的。

C:/Windows/System32/drivers/etc/hosts

localhost name resolution is handled within DNS itself.

127.0.0.1       localhost
  1. 重启Apache并测试一切。

原始文章可以找到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.


4
投票

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

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

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


3
投票

从URL中删除公共的4种最佳方法。

如果您使用任何其他技巧从URL中删除公共,例如将server.php的名称更改为index.php并更改为核心文件路径。显然,不要这样做。那么为什么Laravel没有给出这样的解决方案,因为这不是一个正确的方法。

1)使用Laravel中的htaccess从URL中删除public

通过在根目录中添加.htaccess文件,您可以在不公开的情况下访问该网站

.htaccess

2)通过在本地创建虚拟主机来删除公共

我在这里为Window操作系统提供演示。但我会尝试定义一个步骤,以便任何人都可以轻松地按照步骤操作。您还可以针对特定操作系统在Google上进行相同的研究。

步骤1:转到C:\ Windows \ system32 \ drivers \ etc \以管理员模式打开“hosts”文件。

第2步:向其中添加以下代码。在这里,我给你一个projectname.local域名演示的演示,你可以随意指定任何。只需在每个地方保持不变。

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

第3步:现在转到,127.0.0.1 projectname.local 为xampp用户和wamp用户C:\xampp\apache\conf\extra并打开"C:\wamp\bin\apache\Apache2.4.4\conf\extra"文件。现在将以下代码添加到其中。

注意:根据项目更改文档根目录,也可以在定义到“hosts”文件时添加域名。

"httpd-vhosts.conf"

第4步:最后但重要的一步是重新启动您的Xampp或Wamp并访问<VirtualHost projectname.local> ServerAdmin projectname.local DocumentRoot "C:/xampp/htdocs/projectdir" ServerName projectname.local ErrorLog "logs/projectname.local.log" CustomLog "logs/projectname.local.log" common </VirtualHost> 等网址,您的Laravel将在没有公共URL的情况下进行响应。


3)通过在Laravel中运行命令来删除公共

如果您在本地工作,则无需执行任何操作,只需从终端或命令行工具运行以下命令即可。之后,您可以通过命令行提供的URL访问您的网站。

http://projectname.local

如果您愿意在特定IP上运行项目,则需要运行以下命令。如果您正在使用LAN,那么如果您想允许其他人从本地访问您的网站,那么您只需要在命令后运行“ipconfig”运行“ipconfig”后使用命令行检查您的IP地址。

   > php artisan serve

如果您愿意使用特定端口在特定IP上运行项目,则需要执行以下命令。

> php artisan serve --host=192.168.0.177

4)删除托管服务器或cpanel上的公共

> php artisan serve --host=192.168.0.177 --port=77

完成项目后,您需要在服务器上托管项目,然后您只需要将域中的文档根目录设置为公用文件夹。请查看以下屏幕截图。

根据屏幕截图,如果你没有任何项目文件夹进入public_html,那么你只需要设置你的文档根目录,如enter image description here


2
投票

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

"public_html/public"

编辑index.php

    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. 

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

2
投票

首先,您可以使用此步骤

对于Laravel 5:

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

2.将.htaccess文件从/ public目录复制到Laravel根文件夹。

来源:require __DIR__.'/local/bootstrap/autoload.php'; $app = require_once __DIR__.'/local/bootstrap/app.php';

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

解决方案建议:您可以简单地对https://stackoverflow.com/a/28735930功能进行微小改动。

为了这:

  1. 打开helpers::asset
  2. 转到130行
  3. vendor\laravel\framework\src\Illuminate\Foundation\helpers.php而不是"public/".$path$path

110
投票

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

  1. 将server.php重命名为index.php(无修改)
  2. 将.htaccess从公用文件夹复制到根文件夹(如下面的rimon.ekjon所述)
  3. 更改.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] 如果需要任何其他静态文件,只需将扩展名添加到上一个声明的列表中

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

使用激活mod_rewrite模块

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteRule ^(.*)$ public/$1 [L]
    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_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

并重新启动apache

sudo a2enmod rewrite

要从.htaccess文件中使用mod_rewrite(这是一个非常常见的用例),编辑默认的VirtualHost

sudo service apache2 restart

在“DocumentRoot / var / www / html”下面添加以下行:

sudo nano /etc/apache2/sites-available/000-default.conf

再次重启服务器:

<Directory "/var/www/html">
AllowOverride All
</Directory>

1
投票

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


1
投票

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

ln projectname/public project

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


1
投票

Laravel 5.5

在我第一次安装Laravel之后,我遇到了着名的“公共文件夹问题”,我想出了这个解决方案,我个人认为,这个解决方案比我在网上找到的其他人更“干净”。


成就

  • URI中没有<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>
  • 保护public文件免受好奇的人

只需使用.env和四个简单的规则编辑.htaccess即可完成所有工作。


脚步

  1. mod_rewrite文件移动到主根目录中的.htaccess
  2. 编辑如下

我评论了一切,所以对于那些从未使用过public/.htaccess的人(我不是说我是专家,反之亦然)应该清楚(我希望)。另外,为了理解规则,必须清楚的是,在Laravel中,如果Bill连接到mod_rewritehttps://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] ,他的回答应该是被接受的。只有一个简单明了的规则,可以将所有流量重定向到公用文件夹,而无需修改任何文件。


0
投票

您可以使用各种方法从网址中删除公共关键字。

1)如果您使用专用主机并且您具有root访问权限,则可以使用虚拟主机从URL中删除public关键字。您应该使用public提供DocumentRoot路径。所以这将从公共目录启动索引并从url中删除它。

Abhinav Saraswat's solution

2)如果您没有托管对象,那么您应该在根目录中生成一个新的.htaccess文件并将代码放在下面

<VirtualHost *:80>
    ServerAdmin [email protected]
    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>

你可以在这里获得更多<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
投票

我在这里尝试了一些解决方案,发现htaccess适用于laravel 5.2,如下所示:

reference

当你有另一个文件或文件夹被禁止时,只需添加

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
RewriteRule ^.env - [F,L,NC]
RewriteRule ^app - [F,L,NC]
RewriteRule ^bootstrap - [F,L,NC]
RewriteRule ^config - [F,L,NC]
RewriteRule ^database - [F,L,NC]
RewriteRule ^resources - [F,L,NC]
RewriteRule ^vendor - [F,L,NC]

0
投票

你不需要做太多,只需在根目录下创建一个.htaccess文件并粘贴/保存下面的文件;

RewriteRule ^your_file - [F,L,NC]

0
投票

请将这些行添加到root htaccess文件夹中,该文件夹可以完成这项工作

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

-1
投票

我找到了解决这个问题最有效的方法。

只需在根文件夹中编辑RewriteEngine on RewriteCond %{REQUEST_URI} !public/ RewriteRule (.*) /public/$1 [L] 并编写以下代码即可。没有其他要求

.htaccess

56
投票

在Laravel 5.5中,在根目录中创建.htaccess文件并放置以下代码: - qazxsw poi

Reference Link

42
投票

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

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

36
投票

@ Rimon。一个棚子:

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

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

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

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

现在一切正常:)

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


23
投票

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

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

而已!

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

如果要将代码保存在子目录中,则可以创建子域,然后此代码也适用于此。


22
投票

1)我还没有找到一种在L5中移动公共目录的工作方法。虽然你可以修改引导程序<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule> 中的一些东西,但是看起来有几个辅助函数是基于公共目录存在的假设。老实说,你真的不应该移动公共目录。

2)如果您使用MAMP,那么您应该为每个项目创建新的vhost,每个项目都是项目公共目录。创建后,您可以按定义的服务器名称访问每个项目,如下所示:

index.php

16
投票

DON'T!

你真的不应该将你的Laravel根文件夹中的http://project1.dev http://project2.dev 重命名为server.php,并将index.php文件从.htaccess目录复制到你的Laravel根文件夹!

这样每个人都可以访问你的一些文件(例如/public)。亲自尝试一下。你不希望这样!


DO

相反,你应该在你的root中创建一个.env文件,如下所示:

.htaccess

这将默默地将所有基URI重写到RewriteEngine On RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(.*)$ /public/$1 [L,QSA] 文件夹。所有/public,例如Headers和可选的URI参数都将默默地传递到the HTTP Authorization Header文件夹。

而已!


13
投票

可以删除laravel5中的公共URL。跟着这些步骤:

步骤1.从公共中复制所有文件并粘贴到根目录

步骤2.打开/public文件替换

index.php

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

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

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

并删除所有缓存和cookie。

© www.soinside.com 2019 - 2024. All rights reserved.