【问题标题】:Laravel always return 404Laravel 总是返回 404
【发布时间】: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


【解决方案1】:

您需要将您的路由包装在一个前缀为api 的路由组中。例如:

Route::group(['prefix' => 'api'], function(){
    Route::get('/', function()
    {
        return 'Hello World';
    });

    Route::get('/test', function()
    {
        return 'Hello Test';
    });
});

原因是您的 Laravel 应用程序不是在根域中提供,而是在子文件夹中提供。因此,每个请求的URI都会以api开头。

【讨论】:

  • 是的,它的工作!谢谢!我认为组不是必需的,因为我在.htaccess (3) 规则RewriteBase /api/ 中使用。现在我添加组前缀并从.htaccess (3) 中删除RewriteBase /api/。这是最好的解决方案?我问是因为现在api 前缀定义了两次:在.htaccess (1) 和backend/app/Http/routes.php 中。没有任何解决方案只在.htaccess (1) 中定义前缀?
  • 恐怕我们无法从routes.php 文件中删除该组。 RewriteBase 不会去除 URI 前缀。您可以安全地从您的 .htaccess (3) 中删除 RewriteBase
猜你喜欢
  • 2021-05-16
  • 2021-08-05
  • 1970-01-01
  • 2016-02-04
  • 2014-05-29
  • 2021-07-28
  • 2015-11-23
  • 1970-01-01
  • 2020-11-11
相关资源
最近更新 更多