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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24784606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:53:19  来源:igfitidea点击:

Laravel Routes not working, Apache configuration only allows for public/index.php/route

apache.htaccesslaravel

提问by user3817533

Example:

例子:

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

To view the route above, I must navigate to public/index.php/get.

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

I've viewed quite a few SO posts and googled around trying different things and it hasn't made a difference (yes I restart apache every time).

我已经查看了很多 SO 帖子并在 google 上尝试了不同的东西,但并没有产生任何影响(是的,我每次都重新启动 apache)。

Here is my .htaccess in the public directory:

这是我在公共目录中的 .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/ [L]
</IfModule>

What could be causing this still? I'm running Ubuntu.

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

采纳答案by Eduardo Reveles

Do you have mod_rewrite installed and enabled on apache? Try to remove the if lines ( and ) and see if it throws an error when you try to load the website. If it does, run sudo a2enmod rewriteand restart apache.

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

This is the .htaccess I have on my public/ directory:

这是我在 public/ 目录中的 .htaccess:

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

    RewriteEngine On

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

回答by peter.babic

Two most common causes of this behavior are:

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

  1. mod_rewrite not enabled

    sudo a2enmod rewrite && sudo service apache2 restart

  2. AllowOverrideis set to None, set it to All, assuming Apache2.4

    sudo nano /etc/apache2/apache2.conf

  1. 未启用 mod_rewrite

    sudo a2enmod rewrite && sudo service apache2 restart

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

    sudo nano /etc/apache2/apache2.conf

search for <Directory /var/www/>and change AllowOverride Noneto AllowOverride All, then save the file and restart apache

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

回答by Mr CaT

Changing AllowOverride Noneto AllowOverride Alland sudo a2enmod rewrite && sudo /etc/init.d/apache2 reloaddid help.

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

回答by Tantrik

In the Apache Configuration filemay 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:

Apache 配置文件可能位于/etc/httpd/conf/httpd.conf(位置和名称因服务器而异)中,通过更新目录为 .htaccess 文件提供 AllowOverride 权限,如下所示:

<Directory "/var/www/laravel-sample-project/public">
   AllowOverride All
</Directory>

This may solve your problem on laravel routes.

这可能会解决您在 Laravel 路线上的问题。

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)

还要确保您提供仅限于目录的“AllowOverride All”访问权限,例如上面提到的并且仅限于目录/var/www/laravel-sample-project/public(这里提到的是我的项目的根目录,可能会有所不同为你)

回答by JVitela

If you do not use a .htaccess file, you need to specify the directory for your redirections:

如果不使用 .htaccess 文件,则需要为重定向指定目录:

  <IfModule mod_rewrite.c>
     RewriteEngine On

     <Directory /home/vitela/laravel/public>
      RewriteBase /public
      RewriteRule ^(.*)/$ / [L,R=301]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /index.php [L,QSA]
     </Directory>
  </IfModule>

回答by Pramod yadav

In MacOs. The Main problem of .htaccess not working is there is not mod_rewrite.so enabled in httpd.conf file of apache configuration. i have solved this by uncomment the line :

在 MacOs 中。.htaccess 不工作的主要问题是在 apache 配置的 httpd.conf 文件中没有启用 mod_rewrite.so。我已经通过取消注释该行解决了这个问题:

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

Remove the # from above line httpdf.conf. Then it will works. enjoy!

从上面的行 httpdf.conf 中删除 #。然后它会起作用。请享用!