【问题标题】:how to exclude www from dynamically created subdomain如何从动态创建的子域中排除 www
【发布时间】:2016-01-05 09:30:58
【问题描述】:

在用户注册后的 Laravel 应用程序中,我根据用户名为每个用户分配一个子域。

如果用户想访问非法且未注册的子域,我会显示 404 错误页面。

因为这是我的路线:

Route::group(['domain' => Config::get('app.url')], function () {
     //some Other Routes for all Members
});


// Match a subdomain of my domain
Route::group(['domain' => '{username}.' . Config::get('app.url')], function () {
    Route::get('/', ['uses' => 'DashboardController@home']);
    Route::get('/news/{news_alias}', ['uses' => 'DashboardController@news_details']);
});

config/app.php 中,我有这样的 url 选项:

'url'             => 'tc.dev'

一切正常但我的问题是当用户尝试打开 www.tc.dev 域时,laravel 显示 404 错误页面 因为 laravel 认为这是一个用户名和找不到它并显示错误页面。

如何从子域中排除 www 以便充当普通域。

【问题讨论】:

  • “动态创建的子域” - 大概这是一个 通配符 子域,或者您是否真的在 DNS 中动态创建它?
  • 你可以使用 .htaccess 重定向它
  • @w3d,即通配符子域。
  • 虽然如果用户确实访问了www.user.example.com,那么应该真的会导致404,还是最好重定向到user.example.comwww.tc.dev 是什么 - 这是您的主要域名吗?您的主域是否只是 example.com(即没有 www 子域)?
  • 是的,如果用户想访问www.user.example.com,最好重定向到user.example.comwww.tc.dev 是我的主域,必须被视为tc.dev

标签: php .htaccess laravel


【解决方案1】:

我没有使用过 Laravel,但我认为这可以解决问题。

$routerFunction = function() {
    //some Other Routes for all Members
};

Route::group(['domain' => Config::get('app.url')], $routerFunction);
Route::group(['domain' => 'www.' . Config::get('app.url')], $routerFunction);


// Match a subdomain of my domain
Route::group(['domain' => '{username}.' . Config::get('app.url')], function () {
    Route::get('/', ['uses' => 'DashboardController@home']);
    Route::get('/news/{news_alias}', ['uses' => 'DashboardController@news_details']);
});

【讨论】:

  • 是的,确实很好用。这是最简单的正确答案。
猜你喜欢
  • 2018-09-18
  • 1970-01-01
  • 2011-08-14
  • 1970-01-01
  • 2017-08-28
  • 2015-07-29
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多