spatie / laravel-missing-page-redirector
在您的 Laravel 应用程序中重定向丢失的页面
Requires
- php: ^8.0|^8.1
- laravel/framework: ^8.28|^9.0|^10.0|^11.0
- spatie/url: ^1.0|^2.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- pestphp/pest: ^1.22|^2.34
- phpunit/phpunit: ^9.0|^9.3|^10.5
README
当从旧网站迁移到新网站时,您的 URL 可能会发生变化。如果您的旧网站很受欢迎,您可能希望保留您的 SEO 价值。实现这一目标的一种方法是从旧 URL 永久重定向到新 URL。此包使此过程变得非常简单。
安装后,您只需要将重定向添加到配置文件中。
支持我们
我们投入了大量资源来创建最好的开源包。您可以通过购买我们的付费产品来支持我们。
我们非常感谢您从您的家乡寄来明信片,注明您正在使用我们的哪些包。您可以在我们的联系页面上找到我们的地址。我们将发布所有收到的明信片在我们的虚拟明信片墙上。
安装
您可以通过 composer 安装此包。
composer require spatie/laravel-missing-page-redirector
包将自动注册自己。
接下来,将 Spatie\MissingPageRedirector\RedirectsMissingPages
中间件添加到您的全局中间件堆栈中。
// bootstrap/app.php ->withMiddleware(function (Middleware $middleware) { $middleware->append([ \Spatie\MissingPageRedirector\RedirectsMissingPages::class, ]); })
最后,您必须发布配置文件。
php artisan vendor:publish --provider="Spatie\MissingPageRedirector\MissingPageRedirectorServiceProvider"
这是已发布的配置文件的内容
return [ /* * This is the class responsible for providing the URLs which must be redirected. * The only requirement for the redirector is that it needs to implement the * `Spatie\MissingPageRedirector\Redirector\Redirector`-interface */ 'redirector' => \Spatie\MissingPageRedirector\Redirector\ConfigurationRedirector::class, /* * By default the package will only redirect 404s. If you want to redirect on other * response codes, just add them to the array. Leave the array empty to redirect * always no matter what the response code. */ 'redirect_status_codes' => [ \Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND ], /* * When using the `ConfigurationRedirector` you can specify the redirects in this array. * You can use Laravel's route parameters here. */ 'redirects' => [ // '/non-existing-page' => '/existing-page', // '/old-blog/{url}' => '/new-blog/{url}', ], ];
用法
创建重定向很简单。您只需在配置文件中的 redirects
键中添加一个条目。
'redirects' => [ '/non-existing-page' => '/existing-page', ],
您可以使用与使用 Laravel 路由时相同的路由参数。
'redirects' => [ '/old-blog/{url}' => '/new-blog/{url}', ],
可选参数也是可选的
'redirects' => [ '/old-blog/{url?}' => '/new-blog/{url}', ],
最后,您可以使用星号(*
)作为通配符参数,该参数将匹配多个 URL 段(有关更多信息,请参阅Laravel 文档中的编码 URL 斜杠)。当您想要将 URL 如 /old-blog/foo/bar/baz
重定向到 /new-blog/foo/bar/baz
时,这非常有用。
'redirects' => [ '/old-blog/*' => '/new-blog/{wildcard}', // {wilcard} will be the entire path ],
默认情况下,该包仅在请求有 404
响应代码时重定向,但您可以在任何响应代码上重定向。要实现此目的,您可以将 redirect_status_codes
选项更改为响应代码的数组或留空以在发送到 URL 的任何响应代码上重定向。您可以使用以下语法来覆盖此设置
'redirect_status_codes' => [\Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND],
您还可以选择指定在执行重定向时使用的 HTTP 响应代码。默认情况下,设置为 301 Moved Permanently
响应代码。您可以使用以下语法来覆盖此设置
'redirects' => [ 'old-page' => ['/new-page', 302], ],
事件
当找到路由的重定向时,包将触发 RouteWasHit
事件。如果没有找到重定向,将触发 RedirectNotFound
。
创建您自己的重定向器
默认情况下,此包将使用 Spatie\MissingPageRedirector\Redirector\ConfigurationRedirector
,它将从配置文件中获取重定向信息。如果您想使用其他来源的重定向(例如数据库),您可以创建自己的重定向器。
一个有效的重定向器是实现 Spatie\MissingPageRedirector\Redirector\Redirector
接口的任何类。该接口如下所示
namespace Spatie\MissingPageRedirector\Redirector; use Symfony\Component\HttpFoundation\Request; interface Redirector { public function getRedirectsFor(Request $request): array; }
getRedirectsFor
方法应返回一个数组,键是旧网址,值是新网址。
如果您想使用 Route::fallback
如果您不想覆盖默认的重定向器,或者您已经有了基于 Laravel 文档 的现有 Route::fallback
逻辑,您可以使用此包如下。在您的 web.php
文件底部,
use Spatie\MissingPageRedirector\MissingPageRouter; //... Your other route Route::fallback(function (Request $request) { $redirectResponse = app(MissingPageRouter::class)->getRedirectFor($request); if ($redirectResponse !== null) { return $redirectResponse; } //... Your other logic });
您可以根据需要调整重定向的优先级。
更新日志
请参阅 更新日志 了解最近的变化信息。
测试
$ composer test
贡献
有关详细信息,请参阅 贡献指南。
安全
如果您发现与安全相关的错误,请通过 security@spatie.be 发送邮件,而不是使用问题跟踪器。
鸣谢
许可协议
MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件。