没有index.php的路由不起作用

问题描述 投票:11回答:6

我正在使用laravel 4。

我有一个视图nest.blade.php和相应的控制器NestController.php:

控制器内容:

class NestController extends BaseController {

    public function showView()
    {
        return View::make('nest');
    }

}

路线:

Route::get('/nest', 'NestController@showView');

当我去url / nest它不起作用。当我去url / index.php / nest时,它确实有效。

显然我只想让它成为/ nest而不使用index.php。

我该如何解决这个问题?

我的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>
php laravel laravel-routing
6个回答
8
投票

Pretty URLs

该框架附带一个public / .htaccess文件,用于允许没有index.php的URL。如果您使用Apache来为Laravel应用程序提供服务,请确保启用mod_rewrite模块。

如果Laravel附带的.htaccess文件不能与您的Apache安装一起使用,请尝试以下方法:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

有关更多相关帮助,请查看this answer


3
投票

我在Apache HTTP服务器中启用了mod_rewrite模块,重新启动了Apache服务并再次进行了测试并且工作正常!

下面是我用来启用mod_rewrite的代码;

1)在Apache / conf / httpd.conf中取消哈希这一行

LoadModule rewrite_module modules / mod_rewrite.so

2)为“sample-project”创建新配置,而不修改httpd.conf文件中的默认配置

Directory "C:/Apache24/htdocs/sample-project">
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Order allow,deny
  Allow from all
Directory>

2
投票

注意!如果启用重写模块后仍无法正常工作。您需要在httpd.conf中将“AllowOverride None”行更改为“AllowOverride All”。

当@The Alpha警告时,Laravel不需要为此目的进行配置。


2
投票

当您在AllowOverride None中将AllowOverride All行更改为httpd.conf时,请确保在包含有效路径的<directory ...>中执行此操作。

<Directory "/var/www/html"> #check path
   Options Indexes FollowSymLinks
   AllowOverride All
</Directory>

0
投票

在你的.htaccess中设置它

DirectoryIndex index.php

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

RewriteEngine On

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

RewriteRule .? %{ENV:BASE}/index.php [L]


0
投票

由于某些原因,上面的答案对我不起作用,所以这里唯一适合我的答案。

我使用ubuntu 18.04 amd64作为我的开发服务器环境。所以我修改了位于atapache.conf/etc/apache2文件

从:

<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted

至:

<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted

还来自:

<Directory /usr/share> AllowOverride None Require all granted

<Directory /usr/share> AllowOverride All Require all granted

之后,您需要在终端中使用apache命令重新启动sudo service apapche2 restart

现在我可以在不使用URL中的index.php的情况下访问该页面

归功于:joshymatheww @ Laracasts servers discussion channel on Aug 5, 2017

© www.soinside.com 2019 - 2024. All rights reserved.