动态虚拟主机与Laravel产生500错误


Dynamic Virtual Host gives 500 error with Laravel

我正在尝试用虚拟主机创建一个动态开发环境。现在我有:

<VirtualHost *:80>
   UseCanonicalName Off
   VirtualDocumentRoot "C:'xampp'htdocs'%1'public"
   # available aliases to use
   ServerAlias *.dev
</VirtualHost>

它适用于Laravel的默认路线:

Route::get('/', function () {
    return view('home.index');
});

但任何其他路由都会给我500个内部重定向错误。我使用Laravel 5.2,除了routes.php和一些默认更改外,没有任何更改。

当我将这样的域分配为VirtualHost时,一切都很好:

<VirtualHost *:80>
  ServerName example.dev
  VirtualDocumentRoot none
  DocumentRoot "C:'xampp'htdocs'example'public"
</VirtualHost>

我如何解决这个问题,这样我就可以使用动态域名,这样我不必手动添加所有域。

提前感谢您,Stefan Fransen

编辑

当我使用这个:

http://example.dev/index.php/test

页面加载正确,但这不是我想要的。那么我该如何从url中删除index.php呢?我已经检查过了,所有模块都正确加载了,这是我的。htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    RewriteEngine On
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]
#    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
#    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

编辑

发现当我将RewriteRule ^ index.php [L]更改为RewriteRule ^(.*)$ /index.php/$1 [L]

但我仍然不明白为什么它在手动添加的vhost上有效,而在动态生成的vhost中无效,有人对此有解释吗?

我猜您的重写模块(mod_rewrite)已禁用。在apache.conf 中检查

也许Pretty URL部分可以帮助你。https://laravel.com/docs/5.1

我遇到了同样的问题。我找到了解决方案。

转到根目录上的.htaccess文件,如果它不在根目录中,那么它将在从那里剪切的公共文件夹中可用,并粘贴到根目录中

更改以下代码。

重写规则^index.php[L]

重写规则^(.*)$/index.php/$1[L]