【发布时间】:2015-07-31 00:40:57
【问题描述】:
我使用 Laravel 作为后端(API REST)和 AngularJS 作为前端(使用 API)。 我想要重定向:
/api --> /backend/public (Laravel)
/ --> /frontend/app (AngularJs)
前端运行没有问题,但 Laravel 总是为现有路由返回 404(RouteCollection.php 中的 NotFoundHttpException)。
我哪里出错了?
我的文件夹结构:
/var/www
-- .htaccess (1)
-- frontend
---- app
------ index.html
------ .htaccess (2)
-- backend
---- public
------ index.php
------ .htaccess (3)
.htaccess (1)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Backend
RewriteRule ^api(.*) backend/public/$1 [L]
# Frontend
RewriteRule ^(.*) frontend/app/$1 [L]
</IfModule>
.htaccess (2)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L]
</IfModule>
.htaccess (3)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /api/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Apache 配置
<VirtualHost *:80>
...
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
...
</VirtualHost>
backend/app/Http/routes.php的一部分
<?php
Route::get('/', function()
{
return 'Hello World';
});
Route::get('/test', function()
{
return 'Hello Test';
});
每个对后端 (http://domain.com/api*) 的请求都从 Laravel 返回 NotFoundHttpException in RouteCollection.php line 145(所以 backend/public/index.php 正在运行),例如:
http://domain.com/api
http://domain.com/api/test
感谢您的宝贵时间。
【问题讨论】:
-
也许打开rewrite debug logging,这样您就可以看到发生了什么?但是,假设 .htaccess 重写都正常工作,问题可能是您的 URL 将出现在 Laravel 中以
api/为前缀,而您的路由文件不包含这些前缀? -
您有任何带有前缀属性的路由组吗?通过 htaccess 重定向后,请求的 URI 将被更改,并且有时您注册了错误的路由。您可以发布您的测试 URL 和注册该路由的 routes.php 文件的一部分吗?如果您提供更多数据,我将删除我的旧答案并继续测试您的案例。
-
@hieu-le 谢谢,我更新了上面的问题描述。
标签: apache .htaccess mod-rewrite laravel