除了 root 之外,Laravel 路由不起作用

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

Laravel routes not working except for root

phpapache.htaccesslaravel

提问by Kevin Joymungol

I have been spending hours on this issue and hope to find my way out. I have set up laravel correctly, created a project myapp

我一直在这个问题上花了几个小时,希望能找到出路。我已经正确设置了 laravel,创建了一个项目 myapp

My route.php file just contains

我的 route.php 文件只包含

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

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

When I run

当我跑

http://localhost/myapp/public/

I get the laravel start page correctly

我正确获得了 Laravel 起始页

When I run

当我跑

http://localhost/myapp/public/users

I get

我得到

The requested URL /myapp/index.php was not found on this server.

I don't know why its looking for index.php.

我不知道它为什么要寻找 index.php。

When I run

当我跑

http://localhost/myapp/public/index.php/users

I get a page with text "Users". I should obtain this page when running

我得到一个带有“用户”文本的页面。我应该在运行时获得这个页面

http://localhost/myapp/public/users

instead.

反而。

Below is my .htaccess

下面是我的 .htaccess

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

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

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

Any ideas? Am running a Apache on Linux.

有任何想法吗?我在 Linux 上运行 Apache。

回答by alexrussell

Your RewriteBaseis set to /myapp/but your index.php file is in /mayapp/public/is it not?

RewriteBase已设置为/myapp/但您的 index.php 文件在/mayapp/public/,不是吗?

As such, I think you'll find the RewriteBaseneeds to be /myapp/public/.

因此,我认为您会发现RewriteBase需要是/myapp/public/.

回答by JustinHo

The quickest way is to add a symbolic link in command line:

最快的方法是在命令行中添加符号链接:

ln -s your/acutal/path/myapp/public app-dev

Now browse http://localhost/app-devshould work fine as well as all the routes. You can change app-dev to whatever you want as the name.

现在浏览http://localhost/app-dev应该可以正常工作以及所有路线。您可以将 app-dev 更改为您想要的任何名称。

回答by Elshan

Just do this and try to restart the apache.

只需这样做并尝试重新启动apache。

sudo a2enmod rewrite

To restart apache: sudo services apache2 restart

要重新启动 apache: sudo services apache2 restart

回答by Tom

In your application/config/application.php file change:

在您的 application/config/application.php 文件中更改:

'index' => 'index.php',

to:

到:

'index' => '',

That should tell laravel not to use index.php and to rewrite correctly.

这应该告诉 laravel 不要使用 index.php 并正确重写。

It works with standard .htaccess file which comes with laravel (haven't checked if you modified it)

它适用于 laravel 附带的标准 .htaccess 文件(尚未检查是否修改过)

回答by ajtrichards

Use this re-write rule:

使用这个重写规则:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteBase /my_app
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Also, in `application/config/app.php' change:

此外,在“application/config/app.php”中更改:

'index' => 'index.php'

'index' => 'index.php'

to

'index' => ''

'index' => ''

回答by Valdas

  1. Make sure you have mod_rewrite enabled on apache web server.
  2. Make sure that your vhost config allows and parses htaccess files
  1. 确保您在 apache 网络服务器上启用了 mod_rewrite。
  2. 确保您的 vhost 配置允许并解析 htaccess 文件