検索エンジン最適化(SEO)を最適化するには、先週、小文字に相当する大文字を含むすべてのリクエストをリダイレクトする必要がありました。
例:
源 URL | 目标 URL |
---|---|
/location/Atlanta | /location/atlanta |
/docs/Laravel-Middleware | /docs/laravel-middleware |
同時に、このソリューションはクエリパラメーターを変更しないでください。
源 URL | 目标 URL |
---|---|
/locations/United-States?search=Georgia | /location/united-states?search=Georgia |
メソッドを使用して、クエリ文字列をパスの小文字バージョンに戻し、小文字パスに永続的にリダイレクトできます。 url()->query()
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; class RedirectUppercase { /** * 处理传入请求。 * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle(Request $request, Closure $next): Response { $path = $request->path(); if (($lower = strtolower($path)) !== $path) { $url = url()->to($lower)->appendQuery($request->query()); // 使用更简洁的链式调用 return redirect($url, 301); } return $next($request); } }
ミドルウェアグループに添付しました。 bootstrap/app.php
web
<?php return Application::configure(basePath: dirname(__DIR__)) ->withRouting( // ... ) ->withMiddleware(function (Middleware $middleware) { $middleware->appendToGroup('web', \App\Http\Middleware\RedirectUppercase::class); });
nginxまたはapacheで行うことができると確信していますが、これは最も簡単なソリューションであり、アプリケーションのすべての環境で機能します。新しいサーバーで変更を行うことを忘れないでください。
以上が大文字のURLをLaravelミドルウェアで小文字にリダイレクトする方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。