higolas
higolas

Reputation: 11

How to use Subdomain replace for subdirectory in laravel 5

I am using laravel 5.2 with XAMPP in Windows and I want to use subdomain instead of subdirectory, for example, admin.example.com (for all admin things) instead of example.com/admin. I think this will be great cause I can seperate front-end and back-end so far.

I created a virtual host and named it "example.com" alreary in apache, I changed my hosts file:

127.0.0.1 example.com
127.0.0.1 admin.example.com

And in routes.php file in laravel 5.2

Route::get('/', function() {
    return view('welcome');
});

Route::group(['domain' => 'admin.example.com'], function() {
    Route::get('login', 'AdminController@showLoginForm');
)};

But I still got the view "welcome" from laravel when I think it should be the Login form though I called the url admin.example.com in browser.

So can anyone tell me what I did wrong or having any ideas about this? Thank you very much.

@Mehul Kuriya, this is my .htaccess, it's original from installing laravel

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Upvotes: 1

Views: 612

Answers (1)

jaysingkar
jaysingkar

Reputation: 4435

Problem is in your route file. Considering you want to use two sub domains i.e. www(example.com) and admin(admin.example.com). You would need to specify the routes like below:

Route::group(['domain' => 'www.example.com'], function() {
        Route::get('/', function() {
        return view('welcome');
    });
)};

Route::group(['domain' => 'admin.example.com'], function() {
    Route::get('login', 'AdminController@showLoginForm');
)};

Upvotes: 1

Related Questions