如何使用Laravel路由系统将所有Laravel路由重定向到一个新的子域名?

huangapple go评论67阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月22日 21:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定