laravel 没有 index.php 的路由不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21662378/
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-14 09:00:36  来源:igfitidea点击:

Routes not working without index.php

phplaravellaravel-routing

提问by RSM

Im using laravel 4.

我使用 Laravel 4。

I have a view nest.blade.phpand the corresponding controller NestController.php:

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

Controller content:

控制器内容:

class NestController extends BaseController {

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

}

Route:

路线:

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

When I go to url/nest it doesnt work. When I go to url/index.php/nest it does work.

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

Obviously I just want it to be /nest without the index.php.

显然我只是希望它是没有 index.php 的 /nest。

How can i resolve this?

我该如何解决这个问题?

My htaccess:

我的 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 The Alpha

Pretty URLs

漂亮的网址

The framework ships with a public/.htaccess file that is used to allow URLs without index.php. If you use Apache to serve your Laravel application, be sure to enable the mod_rewrite module.

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

If the .htaccessfile that ships with Laravel does not work with your Apache installation, try this one:

如果.htaccessLaravel 附带的文件不适用于您的 Apache 安装,请尝试以下操作:

Options +FollowSymLinks
RewriteEngine On

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

For more related help, check this answer.

如需更多相关帮助,请查看此答案

回答by developer86

I enabled the mod_rewrite module in the Apache HTTP server, restarted Apache service and tested again and it WORKED!!!

我在 Apache HTTP 服务器中启用了 mod_rewrite 模块,重新启动了 Apache 服务并再次测试,它起作用了!!!

Below is the code i used to enable mode_rewrite;

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

1) Un-Hash this line in Apache/conf/httpd.conf

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

LoadModule rewrite_module modules/mod_rewrite.so

LoadModule rewrite_module modules/mod_rewrite.so

2) Create a new configuration for "sample-project" without modifying the default configuration in the httpd.conf file

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

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

回答by Wanderson Xesquevixos de Sique

Be Alert! If it is not working even after enabling rewrite module. You need to change the line "AllowOverride None" to "AllowOverride All" in httpd.conf.

警惕!如果即使在启用重写模块后它也不起作用。您需要将 httpd.conf 中的“AllowOverride None”行更改为“AllowOverride All”。

As @The Alpha alerted, Laravel don't need to be configured for this purpose.

正如@The Alpha 提醒的那样,Laravel 不需要为此目的进行配置。

回答by Max Desmos

When you change the line AllowOverride Noneto AllowOverride Allin httpd.confbe sure that you do it inside <directory ...>that contains valid path.

当您将行更改AllowOverride NoneAllowOverride Allin 时httpd.conf,请确保您在<directory ...>包含有效路径的内部执行此操作。

Example

例子

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

回答by Omid Ahmadyani

set this in your .htaccess

在您的 .htaccess 中设置

DirectoryIndex index.php

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

RewriteEngine On

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

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

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

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

回答by Whales_Corps

For some reasons the answers above are not working for me, so here is the only one that worked for me.

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

I am usingubuntu 18.04 amd64as my development server environment. So I modifiedapache.conffile located at/etc/apache2

我正在ubuntu 18.04 amd64用作我的开发服务器环境。所以我修改了apache.conf位于/etc/apache2

From:

从:

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

To:

到:

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

And Also From:

还有来自:

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

To

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

After that, you need to restart apacheusing sudo service apapche2 restartcommand in the terminal

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

Now I can access the page without using index.php in the URL

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

Credit goes to: joshymatheww@ Laracasts servers discussion channel on Aug 5, 2017

归功于joshymatheww@ Laracasts 服务器讨论频道,2017 年 8 月 5 日

回答by Buddhiprakash Kumawat

Login to the terminal

登录终端

sudo nano /etc/apache2/apache2.conf

After line

后线

<Directory /var/www/>
.
.
.
</Directory>

and paste below code

并粘贴下面的代码

<Directory /var/www/html/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
</Directory>

Now Ctrl+O and then Ctrl+M and then Ctrl+x

现在 Ctrl+O,然后是 Ctrl+M,然后是 Ctrl+x

Now restart apache server

现在重启apache服务器

sudo service apache2 restart

回答by Rasheduzzaman

Try the following .htaccess. I never had any trouble with that on a server or localhost environment.

试试下面的.htaccess。我在服务器或本地主机环境中从来没有遇到过任何问题。

DirectoryIndex index.php

目录索引 index.php

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

        RewriteEngine On

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

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

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

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

    </IfModule>