Laravel, how to redirect as 301 and 302

后端 未结 6 1175
轮回少年
轮回少年 2021-02-02 06:46

I cannot find info for redirecting as 301/302 in the Laravel docs.

In my routes.php file I use:

Route::get(\'foo\', function(){ 
    return Redirect::to(         


        
6条回答
  •  面向向阳花
    2021-02-02 07:20

    martinstoeckli's answer is good for static urls, but for dynmaic urls you can use the following.

    For Dynamic URLs

    Route::get('foo/{id}', function($id){ 
        return Redirect::to($id, 301); 
    });
    

    Live Example (my use case)

    Route::get('ifsc-code-of-{bank}', function($bank){ 
        return Redirect::to($bank, 301); 
    });
    

    This will redirect http://swiftifsccode.com/ifsc-code-of-sbi to http://swiftifsccode.com/sbi

    One more Example

    Route::get('amp/ifsc-code-of-{bank}', function($bank){ 
        return Redirect::to('amp/'.$bank, 301); 
    });
    

    This will redirect http://amp/swiftifsccode.com/ifsc-code-of-sbi to http://amp/swiftifsccode.com/sbi

提交回复
热议问题