我已经设置了通配符子域 *.domain.com & 我正在使用以下 .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www\.
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule .* index.php?username=%1 [L]
一切都很完美。
我想在laravel中实现这个方法。主要是我想在您访问 username.domain.com 时显示我的用户个人资料。有什么想法可以实现这一目标吗?
这很容易。首先 - 不要改变.htaccess
Laravel 提供的默认文件。默认情况下,所有对您域的请求都将路由到您的index.php
文件,这正是我们想要的。
然后在你的routes.php
文件仅使用“之前”过滤器,该过滤器在完成其他操作之前过滤对应用程序的所有请求。
Route::filter('before', function()
{
// Check if we asked for a user
$server = explode('.', Request::server('HTTP_HOST'));
if (count($server) == 3)
{
// We have 3 parts of the domain - therefore a subdomain was requested
// i.e. user.domain.com
// Check if user is valid and has access - i.e. is logged in
if (Auth::user()->username === $server[0])
{
// User is logged in, and has access to this subdomain
// DO WHATEVER YOU WANT HERE WITH THE USER PROFILE
echo "your username is ".$server[0];
}
else
{
// Username is invalid, or user does not have access to this subdomain
// SHOW ERROR OR WHATEVER YOU WANT
echo "error - you do not have access to here";
}
}
else
{
// Only 2 parts of domain was requested - therefore no subdomain was requested
// i.e. domain.com
// Do nothing here - will just route normally - but you could put logic here if you want
}
});
编辑:如果您有国家/地区扩展(即domain.com.au或domain.com.eu),那么您将需要更改计数($server)以检查4,而不是3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)