Laravel 路线未投入生产
Laravel routes not working on production
我有一个 Laravel5/angularJS 应用程序(Laravel 作为 api 休息和 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 ^(.*)$ / [L]
RewriteCond %{HTTP_HOST} laravelAppDomain$ [NC]
RewriteCond %{REQUEST_URI} !^/laravel-frontend/.*$
RewriteRule ^(.*)$ /laravel-frontend/ [L]
</IfModule>
index.php 里面 laravel-frontend:
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 文件夹主机、虚拟主机或类似文件。
假设您在 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
laravel-app
public
标准 laravel 方式。
然后我配置指向 public 的 .htaccess,请求开始工作。
我有一个 Laravel5/angularJS 应用程序(Laravel 作为 api 休息和 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 ^(.*)$ / [L]
RewriteCond %{HTTP_HOST} laravelAppDomain$ [NC]
RewriteCond %{REQUEST_URI} !^/laravel-frontend/.*$
RewriteRule ^(.*)$ /laravel-frontend/ [L]
</IfModule>
index.php 里面 laravel-frontend:
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 文件夹主机、虚拟主机或类似文件。
假设您在 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
laravel-app
public
标准 laravel 方式。
然后我配置指向 public 的 .htaccess,请求开始工作。