Jimmy Howe
Jimmy Howe

Reputation: 185

Laravel / Host Setup for Multi-TLD

Basically Ive written out this about 5 times, still dont know how to properly ask, so here goes...

I want two domains, say example.net and example.info which I have registered. I want them to point to the one application I'm building, then hopefully use Laravel to route the two different domains to there own pages... here is what I've tried and what problems I've ran into...

** #1 Domain Forward **

Uploaded my app to example.net and forwarded the .info domain to the .net domain. And then tried...

Route::group(array('domain' => 'example.info'), function()
{
    Route::get('/', function(){ return 'INFO PAGE!'; });
});

Route::group(array('domain' => 'example.net'), function()
{
    Route::get('/', function(){ return 'NET PAGE!'; });
});

Route::get('/', 'HomeController@index');

Problems

  1. Laravel Domain Routing doesnt seem to recognise domains without subdomains, eg '{subdomain}.example.net' will work, but 'example.net' and 'example.info' wont?

  2. When domain forwarding, the HTTP_HOST will always show .net and not .info, so cant use if or switch statement in the routes file

Alternative Soloution

The only soloution i can think of is to use my host's shared hosting setup to have two individual sites, each with the appropriate domains pointing to them and set my IDE to upload to both sites?

Surely there is a better way to do this tho?

Any help would be great, also Im pretty new to this so if you could keep it simple please, thanks... jimmy

Upvotes: 0

Views: 1915

Answers (3)

Jimmy Howe
Jimmy Howe

Reputation: 185

Thanks for the replies guys, this is what i ended up doing... just putting the routes into variable and passing it to route groups, seems to work for me

$frontendRoutes = function()
{
    Route::get('/', function(){
        return 'Index Page';
    })
};

Route::group(['domain' => 'example.net'], $frontendRoutes);
Route::group(['domain' => 'example.info'], $frontendRoutes);

Upvotes: 2

Björn Hjorth
Björn Hjorth

Reputation: 2888

This might be a crazy hack, but as I ended up with the same issue and I wanted to have two tld's pointing to my site and then serve a different language depending on what tld they were coming from I made this solution.

I created a folder called Routes in the app folder.

I then added two folders in the the Routes folder, one named "com" and one named "se".

And in each folder I added the files I wanted to use depending on what language. in the "com" folder I made a file called pages.php with the code:

Route::get('/', 'PagesController@index');

Route::get('about', [
    'as' => 'about',
    'uses' => 'PagesController@about'
]);

And then I added a file in the "se" folder also named pages.php with the code:

Route::get('/', 'PagesController@index');

Route::get('om-oss', [
    'as' => 'about',
    'uses' => 'PagesController@about'
]);

Then back at the routes.php I added this code:

$tld = strrchr ( $_SERVER['SERVER_NAME'], "." );
$tld = substr ( $tld, 1 );

foreach (File::allFiles(__DIR__.'\Routes\/'.$tld) as $partial) 
{
require_once $partial->getPathname();
}

So now I have one folder for each tld, not the best solution but it works.

Upvotes: 0

Barry Pickerall
Barry Pickerall

Reputation: 1

This seems to work but the php artisan routes looks a bit wrong.

http://ryantablada.com/post/multi-tld-routing-in-laravel

I was having a problem having the code pick up the last tld in the list. This, although not perfect, seems to be the best answer I have found so far.

Upvotes: 0

Related Questions