I\'ve set up a wildcard subdomain *.domain.com & i\'m using the following .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HT
While I can't say what the full solution would be in your case, I would start with the SERVER_NAME value from the request (PHP: $_SERVER['SERVER_NAME']) such as:
$username = str_replace('.domain.com', '', Request::server('SERVER_NAME'));
Make sure you additionally clean/sanitize the username, and from there you can lookup the user from the username. Something like:
$user = User::where('username', '=', $username)->first();
Somewhere in the routes file you could conditionally define a route if the SERVER_NAME isn't www.domain.com, or domain.com, though I'm sure others can come up with a much more eloquent way for this part...
Laravel 4 has this functionality out of the box:
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('user/{id}', function($account, $id) {
// ...
});
});
Source
This is easy. Firstly - do NOT change the .htaccess
file from the default provided by Laravel. By default all requests to your domain will be routed to your index.php
file which is exactly what we want.
Then in your routes.php
file just use a 'before' filter, which filters all requests to your application before anything else is done.
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
}
});
edit: if you have a country extension (i.e. domain.com.au or domain.com.eu) then you will want to change the count($server) to check for 4, not 3
ability add subdomains like subdomain *. domain.com should be switched by your hosting provider, in the .htaccess you can not configure support of subdomains