Laravel Routes不工作,Apache配置只允许public/index.php/route

use*_*533 20 apache .htaccess laravel

例:

Route::get('/get', function() {
    return 'get';
});
Run Code Online (Sandbox Code Playgroud)

要查看上面的路由,我必须导航到public/index.php/get.

我已经查看了很多SO帖子,并搜索了不同的东西,并没有产生任何影响(是的,我每次都重启apache).

这是我在公共目录中的.htaccess:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
</IfModule>

# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)

还有什么可能导致这种情况?我正在运行Ubuntu.

del*_*ord 69

导致此行为的两个最常见原因是:

  1. mod_rewrite未启用

    sudo a2enmod rewrite && sudo service apache2 restart

  2. AllowOverride 设置为None,将其设置为All,假设为Apache2.4

    sudo nano /etc/apache2/apache2.conf

搜索<Directory /var/www/>并更改AllowOverride NoneAllowOverride All,然后保存文件并重新启动apache

  • 这甚至在一年半之后帮助了我.谢谢!:) (2认同)

Edu*_*les 23

你在apache上安装并启用了mod_rewrite吗?尝试删除if行(和),并在尝试加载网站时查看是否抛出错误.如果是,请运行sudo a2enmod rewrite并重新启动apache.

这是我公共/目录中的.htaccess:

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)


小智 5

更改AllowOverride NoneAllowOverride Allsudo a2enmod rewrite && sudo /etc/init.d/apache2 reload确实有帮助。


Tan*_*rik 5

In the Apache Configuration file may be located at /etc/httpd/conf/httpd.conf (location and names varies on server), provide AllowOverride permission for .htaccess file by updating the directory as follows:

<Directory "/var/www/laravel-sample-project/public">
   AllowOverride All
</Directory>
Run Code Online (Sandbox Code Playgroud)

This may solve your problem on laravel routes.

Also ensure that you're providing access "AllowOverride All" limited to directories, such as mentioned above and limited to the directory /var/www/laravel-sample-project/public (mentioned here is the root directory for my project which may varies for you)