子域路由在 Laravel 5 上不起作用 - WAMPServer

Subdomain routing not working on Laravel 5 - WAMPServer

我正在尝试在我的应用程序中实现静态和动态子域路由。它没有按预期工作。我在本地机器上使用 WAMPServer。

routes.php

Route::get('/', 'WelcomeController@index');

Route::group(['domain' => 'api.letsplay.dev'], function () {

    Route::group(['prefix' => 'v1'], function () {
        Route::get('users', function () {
            return "Success";
        });
    });

});

php artisan route:list 给出这个

+------------------+----------+----------+------+----------------------------------------------+------------+
| Domain           | Method   | URI      | Name | Action                                       | Middleware |
+------------------+----------+----------+------+----------------------------------------------+------------+
|                  | GET|HEAD | /        |      | App\Http\Controllers\WelcomeController@index | guest      |
| api.letsplay.dev | GET|HEAD | v1/users |      | Closure                                      |            |
+------------------+----------+----------+------+----------------------------------------------+------------+

hosts 文件有这个

127.0.0.1       localhost
127.0.0.1       hosp.dev
127.0.0.1       letsplay.dev

我使用laravel框架提供的.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>

httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin webmaster@letsplay.dev
    DocumentRoot "c:/wamp/www/letsplay-web/public"
    ServerName letsplay.dev
    ErrorLog "logs/letsplay.dev-error.log"
    CustomLog "logs/letsplay.dev-access.log" common
</VirtualHost>

当我尝试从我的浏览器中点击 letsplay.dev 时,它按预期工作。但是在尝试点击 api.letsplay.dev/v1/users 时,我在 Chrome 中得到了 ERR_ICANN_NAME_COLLISION 以及来自 IE 的以下错误!

Forbidden error from IE

帮助我了解我错过了什么!

首先启用 Apache 模块 alias_modulevhost_alias_module

然后在您的 httpd-vhosts.conf 文件中添加以下内容。

<VirtualHost *:80>
    ServerName letsplay.dev
    ServerAlias api.letsplay.dev
    DocumentRoot "c:/wamp/www/letsplay-web/public"
    <directory "c:/wamp/www/letsplay-web/public">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from all
    </directory>
</VirtualHost>

重新启动 WampServer。

然后在您的 hosts 文件中添加以下内容。

127.0.0.1 api.letsplay.dev

检查:icannwiki

.dev 是新提议的 gTLD 之一。我们以前在内部使用 .dev 域,但为了避免问题而转移到 .local。

此外,正如 chanafdo 在他的评论中提到的,您不能在 windows 主机文件中使用通配符。因此,您还必须指定每个子域。

并且您通常应该避免在您的主机文件中使用相同 IP 地址的多行,只需将它们添加到同一行,以空格分隔:

127.0.0.1 localhost letsplay.dev api.letsplay.dev

要在 apache 中启用通配符子域支持,只需指定

ServerAlias *.letsplay.dev

在您的虚拟主机配置中。