Username as subdomain on laravel
Asked Answered
M

4

11

I've set up a wildcard subdomain *.domain.com & i'm using the following .htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !www\.
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule .* index.php?username=%1 [L]

Everything works perfectly.

I want to implement this method in laravel. Mainly I want to have my user's profile displayed when you go to username.domain.com. Any ideas on achieving this?

Maure answered 18/1, 2013 at 14:52 Comment(3)
I supose you are using Laravel 3 ? Or have you started with laravel 4 ?Nightly
I'm still using Laravel 3.Maure
Laravel 4 users: Scroll past the accepted answer.Marigraph
P
24

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

Phenylketonuria answered 22/1, 2013 at 13:57 Comment(4)
Everything works perfectly, but when I access the website using SUBDOMAIN.DOMAIN.COM the Auth component always says I'm not logged in. Any ideas?Maure
Its possible a cookie issue - check this link: forums.laravel.io/viewtopic.php?id=1445Phenylketonuria
Epic, thanks a lot. What if other variables are passed to that url? Such as pages, pagination stuff and so?Maure
It will all route normally based upon your routes, controllers etcPhenylketonuria
M
14

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

Marigraph answered 23/2, 2013 at 16:32 Comment(1)
Do you know how to setup the DNS for this?Irate
L
2

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...

Loess answered 21/1, 2013 at 16:44 Comment(0)
C
0

ability add subdomains like subdomain *. domain.com should be switched by your hosting provider, in the .htaccess you can not configure support of subdomains

Coveney answered 22/1, 2013 at 7:51 Comment(1)
I contacted the hosting support & everything works. But I need an idea how to implement it in laravel 3.Maure

© 2022 - 2024 — McMap. All rights reserved.