I am using the following code its from the laravel 4 site

Route::group(array('domain' => '{account}.myapp.com'), function() {

    Route::get('user/{id}', function($account, $id) {
        // ...
     return Redirect::to('https://www.myapp.com'.'/'.$account);
    });

});

the idea is to redirect subdomain.myapp.com to myapp.com/user/subdomain .what I have is not working any suggestions?sorry I just started with laravel about a month now.

shareimprove this question
 
   
What is the url you are trying? i'm guessing you should remove the user/{id} and replace it with / and then go to fogsy.myapp.com – Marc vd M Jan 24 '14 at 9:41
   
sometimes I can be so dumb... thanks @MarcvdM – cppit Jan 24 '14 at 9:46
   
Do you solve this @cppit ? – Goper Leo Zosa Mar 4 '16 at 5:15

3 Answers 正确答案

Remove user/{id} and replace it with / and then use the following url https://accountname.myapp.com and it will redirect to https://www.myapp.com/accountname

Route::group(array('domain' => '{account}.myapp.com'), function() {

    Route::get('/', function($account, $id) {
        // ...
        return Redirect::to('https://www.myapp.com'.'/'.$account);
    });

});

Edit the answer to the correct answer

shareimprove this answer
 
   
no I did that sorry it was wrong on question – cppit Jan 24 '14 at 9:12
   
sorry this is not the answer, the subdomain is not in used if you redirect it directly – Goper Leo Zosa Mar 4 '16 at 5:13
   
I want to use it to redirect from www.mydomain.com to mydomain.com. It is a 301 redirect? – JCarlos 12 hours ago
Route::group(array('domain' => '{account}.myapp.com'), function() {

    Route::get('/', function($account) {
        // ...
        return Redirect::to('https://www.myapp.com/'.$account);
    });

});

as Marc vd M answer but remove $id from get's closure function.

why use 'https://www.myapp.com'.'/'.$account and not 'https://www.myapp.com/'.$account

shareimprove this answer
 
Route::group(['domain' => '{account}.myapp.com'], function () {
    Route::get('user/{id}', function ($account, $id) {
        //
    });
});

Source : https://laravel.com/docs/5.3/routing#route-group-sub-domain-routing

shareimprove this answer
 

来自  https://stackoverflow.com/questions/21326980/how-to-use-the-laravel-subdomain-routing-function