laravel 5.6 并且 apache 为 url 提供 404

laravel 5.6 and apache gives 404 for urls

我在 Ubuntu 16.04LTS 上有 apache2 服务器,我在 /var/www/html 中有一个名为 book_donation 的 Laravel 5.6 项目,现在当我访问本地主机时 Laravel 欢迎屏幕显示没有问题,但每当我点击任何 url 它给出:

Not Found

The requested URL /book_donation/public/login was not found on this server. Apache/2.4.18 (Ubuntu) Server at 127.0.0.1 Port 80

这里是 .htaccess:

<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>

这是我的 /etc/apache2/site-available/000-default.conf:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    <Directory "/var/www/html">
         AllowOverride all
         Require all granted
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

我的php版本是7.1,apache版本是2.4.18,怎么解决?

docs 所述,您应该将 Web 服务器的文档/Web 根目录配置为 public 目录。

新建文件/etc/apache2/site-available/book_donation.test.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName book_donation.test
    ServerAlias www.book_donation.test
    DocumentRoot /var/www/html/book_donation/public
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

编辑您的 /etc/hosts 文件并添加以下内容:

127.0.1.1   book_donation.test

然后运行:

sudo a2ensite book_donation.test.conf
sudo service apache2 restart

那么您应该可以访问 book_donation.test 并看到您的站点。

我遇到了同样的问题,任何新路由都无法正常工作并出现 404 错误。还要检查 php artisan route:list 新路线详细信息显示正确但仍然无法正常工作。我还尝试了以下命令使其工作

php artisan view:clear php artisan config:clear php artisan cache:clear

这是我的项目详情

我的 DocumentRoot 是 /var/www/html/project/public。当在 web.php 中添加新路由时,它没有工作并给出 404 错误,但根路由工作正常。意味着任何新路线都在运作。所以我做了以下更改,它开始工作了。

在 /etc/apache2/sites-available/project.conf

中更改

<目录/var/www/html/project/public>

**Allowoverride All**

并通过 运行 sudo a2enmod rewrite

启用 url 重写

通过运行重启apache服务器sudo systemctl restart apache2