英文:
How to redirect all Laravel routes to a new subdomain by using laravel route system?
问题
将所有Laravel路由重定向到相同的路由,但更改基本URL。
我想将Laravel项目从域名迁移到子域名。
最佳方法是将最后一个域名上的所有请求重定向到相同的新子域名。
例如,如果用户发送请求到以下URL
mydomain.com/page/1
重定向到以下URL
subdomain.mydomain.com/page/1
我更喜欢在Laravel项目内处理,而不是NGINX配置。
英文:
Redirect all Laravel route to the same route but change base URL.
I want to transfer Laravel project from domain to subdomain
What is the best way to redirect all request on the last domain to the same new subdomain.
for example if user send request to this URL
mydomain.com/page/1
redirect to this URL
subdomain.mydomain.com/page/1
I prefer to handle inside laravel project. not NGINX config.
答案1
得分: 2
To handle this at the Laravel level, you can use a Middleware. Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application.
Here is an example of how you might do it.
First, create a new Middleware by running the command:
php artisan make:middleware SubdomainRedirectMiddleware
Next, open the newly created file app/Http/Middleware/SubdomainRedirectMiddleware.php and add the redirection logic into the handle method:
public function handle(Request $request, Closure $next)
{
// Replace 'mydomain' with your actual domain
if ($request->getHost() === 'mydomain.com') {
// Replace 'subdomain' with your actual subdomain
return redirect()->to(str_replace('mydomain.com', 'subdomain.mydomain.com', $request->fullUrl()));
}
return $next($request);
}
Then, you need to register this middleware. Open app/Http/Kernel.php, add the following line to the routeMiddleware array:
protected $routeMiddleware = [
'subdomain.redirect' => \App\Http\Middleware\SubdomainRedirectMiddleware::class,
];
Route::group(['middleware' => 'subdomain.redirect'], function () {
// All your routes go here
});
Please replace 'mydomain' and 'subdomain' with your actual domain and subdomain in SubdomainRedirectMiddleware.php.
Here is a Reference
https://www.w3schools.in/laravel/middleware
英文:
To handle this at the Laravel level, you can use a Middleware. Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application.
Here is an example of how you might do it.
First, create a new Middleware by running the command:
php artisan make:middleware SubdomainRedirectMiddleware
Next, open the newly created file app/Http/Middleware/SubdomainRedirectMiddleware.php and add the redirection logic into the handle method:
public function handle(Request $request, Closure $next)
{
// Replace 'mydomain' with your actual domain
if ($request->getHost() === 'mydomain.com') {
// Replace 'subdomain' with your actual subdomain
return redirect()->to(str_replace('mydomain.com', 'subdomain.mydomain.com', $request->fullUrl()));
}
return $next($request);
}
Then, you need to register this middleware. Open app/Http/Kernel.php, add the following line to the routeMiddleware array:
protected $routeMiddleware = [
'subdomain.redirect' => \App\Http\Middleware\SubdomainRedirectMiddleware::class,
];
Route::group(['middleware' => 'subdomain.redirect'], function () {
// All your routes go here
});
Please replace 'mydomain' and 'subdomain' with your actual domain and subdomain in SubdomainRedirectMiddleware.php.
▽Here is a Reference
https://www.w3schools.in/laravel/middleware
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论