0

I would like to ask what approach should i use to handle naked domain routing in laravel 5? Given scenario like this, when a user types in example.com, I can use

Route::group(['domain' => 'example.com'], function(){
    Route::get('/', function() {
        return Redirect::to(URL::route('/'));
    });
});

noted that route('/') has been namespaced in the route groups www.example.com

so my question is, what if a user enters example.com/something/very/interesting, how should I redirect the user back to www.example.com/something/very/interesting ? Bare in mind that I would also like to do this to all other actions. Which means at any point of time when a user enters example.com/* it will bring the user back to www.example.com/*

Thank You

Kenny Yap
  • 1,317
  • 5
  • 17
  • 39
  • 3
    You should used [.htaccess](http://www.htaccessbasics.com/force-www-nonwww-domain/) to force user use only www or non-www domain. – Hieu Le Jul 02 '15 at 01:57

1 Answers1

1

This is more effectively solved by your web server rather than at the Laravel level of things. If you are using Apache check out this answer. If you are using Nginx then check out this answer.

You are wanting to force the user to www.example.com so you will need to update your route group to the following;

Route::group(['domain' => 'www.example.com'], function () {
    // The rest of your routes
});
Community
  • 1
  • 1
marcus.ramsden
  • 2,633
  • 1
  • 22
  • 33