【问题标题】:Laravel routes not working on productionLaravel 路线不适用于生产
【发布时间】:2017-02-16 08:47:55
【问题描述】:

我有一个 Laravel5/angularJS 应用程序(将 Laravel 作为前端的 api rest 和 angular)

在我当地的环境中,一切都像魅力一样。

但是当我上传到主机时,我只能访问索引页面,其他所有内容都会抛出 404。

在我的共享主机中,我有这样的文件系统。

public_html
    laravel-backend (it has app, bootstrap, confi...etc, all laravel app)
    laravel-frontend (it would be like the public folder of the laravel default file system)
    .htaccess

.htaccess 内容:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} anotherAppDomainAtSharedServer$ [NC]
    RewriteCond %{REQUEST_URI} !/.*$
    RewriteRule ^(.*)$ /$1 [L]

    RewriteCond %{HTTP_HOST} laravelAppDomain$ [NC]
    RewriteCond %{REQUEST_URI} !^/laravel-frontend/.*$
    RewriteRule ^(.*)$ /laravel-frontend/$1 [L]

</IfModule>

在 laravel-frontend 中的 index.php:

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


$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

和 routes.php

Route::get('/', function () {
    return view('index');
});

Route::group(['prefix' => 'api/v1'], function(){
    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
    ....

所以,正如我所说,我可以看到登录页面,但是当我想登录时,我得到 404 错误

http://laravelAppDomain/laravel-backend/api/v1/authenticate 404 (Not Found)
NotFoundHttpException in RouteCollection.php line 161:

有什么线索吗?有什么我想念的配置吗?

此外,我无法访问配置服务器,我介意我无法像在本地环境中那样编辑 etc 文件夹主机、虚拟主机或类似内容。

【问题讨论】:

    标签: php angularjs .htaccess laravel shared-hosting


    【解决方案1】:

    假设您在 Unix 上的 apache2 服务器上:

    • 运行命令sudo a2enmod rewrite
    • 确保您的/etc/apache2/sites-available/000-default.conf 中有一部分如下所示。注意/var/www/html是apache2的默认根目录,你的可能是/var/www/html/public或类似的东西。

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

    • 运行sudo service apache2 restart

    • 看看这是否有效。

    【讨论】:

    • 我看不到这些文件,我只能访问 public-html 文件夹。
    • 我很确定其他步骤是必要的,但您是否尝试运行 a2enmod 命令并重新启动服务器以检查是否已修复?
    • 我没有 ssh 访问权限,我只能通过 c-panel 进入,所以我无法运行任何控制台命令。
    • 很抱歉,不知道如何帮助您
    • 无论如何,谢谢,我知道你的意思是什么,这正是我在本地环境中所做的,但在这台服务器上,我手头拮据
    【解决方案2】:

    我不确定如何,如果有人可以给我一个理论上的答案,欢迎。

    我更改了文件夹结构:

    public_html
        laravel-app
            public
    

    标准的 laravel 方式。

    然后我将 .htaccess 配置为指向公共,请求开始工作。

    【讨论】:

    • 但是在网络服务器的可公开查看的目录结构中提供完整的laravel-app 是个坏主意。看到这篇文章有一些建议... (laravel-news.com/subfolder-install)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 2016-11-14
    • 2018-02-28
    • 1970-01-01
    • 2018-11-16
    • 2020-04-19
    • 1970-01-01
    相关资源
    最近更新 更多