使用apache2 virtualhost在Laravel 6.4中调用api时出现404错误

我在php 7.2和apache2的ubuntu 18.04上安装了Laravel 6.4。我已经建立了一个案例研究来开发一个api,当我执行 php artisan serve 命令并使用 postman 进行查询时,一切正常。但是,如果我创建了虚拟主机,则可以看到该网站没有问题,但是,通过邮递员执行查询时,出现404错误。 .htaccess文件是默认情况下带laravel的文件:

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

RewriteEngine On

# Handle Authorization Header
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>

我的虚拟主机如下:

<VirtualHost test-api.local:80>
    ServerName test-api.local

    DocumentRoot /var/www/html/restlav/public
    DirectoryIndex index.php

    ErrorLog ${APACHE_LOG_DIR}/test-api.local-error.log
    CustomLog ${APACHE_LOG_DIR}/test-api.local-access.log combined

    <Directory "/var/www/html/restlav">
            Options +FollowSymLinks
            RewriteEngine On
            AllowOverride None
            Require all granted
    </Directory>
</VirtualHost>

我已经尝试过进行正式文档中建议的更改,但一无所获。重写模块已启用。如果有人可以帮助我,我将不胜感激。

kathy86 回答:使用apache2 virtualhost在Laravel 6.4中调用api时出现404错误

部分中,您需要将AllowOverride设置为All:

<VirtualHost test-api.local:80>
    ServerName test-api.local

    DocumentRoot /var/www/html/restlav/public
    DirectoryIndex index.php

    ErrorLog ${APACHE_LOG_DIR}/test-api.local-error.log
    CustomLog ${APACHE_LOG_DIR}/test-api.local-access.log combined

    <Directory "/var/www/html/restlav">
            Options +FollowSymLinks
            RewriteEngine On
            AllowOverride All
            Require all granted
    </Directory>
</VirtualHost>

还要确保在apache服务器中启用重写模块:

$ sudo a2enmod rewrite

别忘了最后重新启动apache服务器,以使更改生效:

$ sudo systemctl restart apache2
本文链接:https://www.f2er.com/3164706.html

大家都在问