redirects
Yönlendirmeler, gelen bir istek yolunu farklı bir hedef yola yönlendirmenize olanak tanır.
Yönlendirmeleri kullanmak için next.config.js adresindeki redirects anahtarını kullanabilirsiniz:
module.exports = {
async redirects() {
return [
{
source: '/about',
destination: '/',
permanent: true,
},
]
},
}redirects source , destination ve permanent özelliklerine sahip nesneleri içeren bir dizinin döndürülmesini bekleyen bir asenkron işlevdir:
sourcegelen istek yolu modelidir.destinationyönlendirmek istediğiniz yoldur.permanenttrueveyafalse- eğertrueistemcilere/arama motorlarına yönlendirmeyi sonsuza kadar önbelleğe almalarını söyleyen 308 durum kodunu kullanacaksa,falsegeçici olan ve önbelleğe alınmayan 307 durum kodunu kullanacaktır.
Next.js neden 307 ve 308 kullanıyor? Geleneksel olarak geçici bir yönlendirme için 302 ve kalıcı bir yönlendirme için 301 kullanılırdı, ancak birçok tarayıcı, orijinal yönteme bakılmaksızın yönlendirmenin istek yöntemini
GETolarak değiştirdi. Örneğin, tarayıcıPOST /v1/usersadresine bir istekte bulunduysa ve bu istek/v2/userskonumuyla302durum kodunu döndürdüyse, sonraki istek beklenenPOST /v2/usersyerineGET /v2/usersolabilir. Next.js, kullanılan istek yöntemini açıkça korumak için 307 geçici yönlendirme ve 308 kalıcı yönlendirme durum kodlarını kullanır.
basePath:falseveyaundefined- false isebasePatheşleştirme sırasında dahil edilmeyecektir, sadece harici yönlendirmeler için kullanılabilir.locale:falseveyaundefined- eşleştirme sırasında yerel ayarın dahil edilmemesi gerekip gerekmediği.hastype,keyvevalueözelliklerine sahip has nesnelerinden oluşan bir dizidir.missingtype,keyvevalueözelliklerine sahip eksik nesnelerden oluşan bir dizidir.
Yönlendirmeler, sayfaları ve /public dosyalarını içeren dosya sisteminden önce kontrol edilir.
Yönlendirmeler, Orta Yazılım mevcut olmadığı ve yolla eşleşmediği sürece istemci tarafı yönlendirmeye (Link, router.push) uygulanmaz.
Bir yönlendirme uygulandığında, istekte sağlanan tüm sorgu değerleri yönlendirme hedefine aktarılır. Örneğin, aşağıdaki yönlendirme yapılandırmasına bakın:
{
source: '/old-blog/:path*',
destination: '/blog/:path*',
permanent: false
}/old-blog/post-1?hello=world talep edildiğinde, istemci /blog/post-1?hello=world adresine yönlendirilecektir.
Path Matching
Yol eşleşmelerine izin verilir, örneğin /old-blog/:slug, /old-blog/hello-world ile eşleşir (iç içe geçmiş yollar yoktur):
module.exports = {
async redirects() {
return [
{
source: '/old-blog/:slug',
destination: '/news/:slug', // Matched parameters can be used in the destination
permanent: true,
},
]
},
}Wildcard Path Matching
Bir joker karakter yolunu eşleştirmek için bir parametreden sonra * kullanabilirsiniz; örneğin /blog/:slug*, /blog/a/b/c/d/hello-world ile eşleşecektir:
module.exports = {
async redirects() {
return [
{
source: '/blog/:slug*',
destination: '/news/:slug*', // Matched parameters can be used in the destination
permanent: true,
},
]
},
}Regex Path Matching
Bir regex yolunu eşleştirmek için regex'i bir parametreden sonra parantez içine alabilirsiniz; örneğin /post/:slug(\\d{1,}), /post/123 ile eşleşir ancak /post/abc ile eşleşmez:
module.exports = {
async redirects() {
return [
{
source: '/post/:slug(\\d{1,})',
destination: '/news/:slug', // Matched parameters can be used in the destination
permanent: false,
},
]
},
}Aşağıdaki karakterler (, ), {, }, :, *, +, ? regex yol eşleştirmesi için kullanılır, bu nedenle source içinde özel olmayan değerler olarak kullanıldıklarında önlerine \\ eklenerek kaçılmalıdır:
module.exports = {
async redirects() {
return [
{
// this will match `/english(default)/something` being requested
source: '/english\\(default\\)/:slug',
destination: '/en-us/:slug',
permanent: false,
},
]
},
}Header, Cookie, and Query Matching
Bir yönlendirmeyi yalnızca başlık, çerez veya sorgu değerleri de has alanıyla eşleştiğinde veya missing alanıyla eşleşmediğinde eşleştirmek için kullanılabilir. Yönlendirmenin uygulanabilmesi için hem source hem de tüm has öğeleri eşleşmeli ve tüm missing öğeleri eşleşmemelidir.
has ve missing öğeleri aşağıdaki alanlara sahip olabilir:
type:String-header,cookie,hostveyaqueryadreslerinden biri olmalıdır.key:String- seçilen türden eşleştirilecek anahtar.value:Stringveyaundefined- kontrol edilecek değer, tanımlanmamışsa herhangi bir değer eşleşecektir. Değerin belirli bir bölümünü yakalamak için regex benzeri bir dize kullanılabilir, örneğinfirst-secondiçinfirst-(?<paramName>.*)değeri kullanılırsa,secondhedefte:paramNameile kullanılabilir.
module.exports = {
async redirects() {
return [
// if the header `x-redirect-me` is present,
// this redirect will be applied
{
source: '/:path((?!another-page$).*)',
has: [
{
type: 'header',
key: 'x-redirect-me',
},
],
permanent: false,
destination: '/another-page',
},
// if the header `x-dont-redirect` is present,
// this redirect will NOT be applied
{
source: '/:path((?!another-page$).*)',
missing: [
{
type: 'header',
key: 'x-do-not-redirect',
},
],
permanent: false,
destination: '/another-page',
},
// if the source, query, and cookie are matched,
// this redirect will be applied
{
source: '/specific/:path*',
has: [
{
type: 'query',
key: 'page',
// the page value will not be available in the
// destination since value is provided and doesn't
// use a named capture group e.g. (?<page>home)
value: 'home',
},
{
type: 'cookie',
key: 'authorized',
value: 'true',
},
],
permanent: false,
destination: '/another/:path*',
},
// if the header `x-authorized` is present and
// contains a matching value, this redirect will be applied
{
source: '/',
has: [
{
type: 'header',
key: 'x-authorized',
value: '(?<authorized>yes|true)',
},
],
permanent: false,
destination: '/home?authorized=:authorized',
},
// if the host is `example.com`,
// this redirect will be applied
{
source: '/:path((?!another-page$).*)',
has: [
{
type: 'host',
value: 'example.com',
},
],
permanent: false,
destination: '/another-page',
},
]
},
}Redirects with basePath support
Yönlendirmelerle basePath desteğinden yararlanırken, yönlendirmeye basePath: false eklemediğiniz sürece her source ve destination 'nin önüne otomatik olarak basePath eklenir:
module.exports = {
basePath: '/docs',
async redirects() {
return [
{
source: '/with-basePath', // automatically becomes /docs/with-basePath
destination: '/another', // automatically becomes /docs/another
permanent: false,
},
{
// does not add /docs since basePath: false is set
source: '/without-basePath',
destination: 'https://example.com',
basePath: false,
permanent: false,
},
]
},
}Redirects with i18n support
Yönlendirmelerle i18n desteğinden yararlanırken, yönlendirmeye locale: false eklemediğiniz sürece her source ve destination yapılandırılmış locales 'u işlemek için otomatik olarak öneklenir. locale: false kullanılıyorsa, doğru şekilde eşleşmesi için source ve destination 'nin önüne bir yerel ayar eklemelisiniz.
module.exports = {
i18n: {
locales: ['en', 'fr', 'de'],
defaultLocale: 'en',
},
async redirects() {
return [
{
source: '/with-locale', // automatically handles all locales
destination: '/another', // automatically passes the locale on
permanent: false,
},
{
// does not handle locales automatically since locale: false is set
source: '/nl/with-locale-manual',
destination: '/nl/another',
locale: false,
permanent: false,
},
{
// this matches '/' since `en` is the defaultLocale
source: '/en',
destination: '/en/another',
locale: false,
permanent: false,
},
// it's possible to match all locales even when locale: false is set
{
source: '/:locale/page',
destination: '/en/newpage',
permanent: false,
locale: false,
},
{
// this gets converted to /(en|fr|de)/(.*) so will not match the top-level
// `/` or `/fr` routes like /:path* would
source: '/(.*)',
destination: '/another',
permanent: false,
},
]
},
}Bazı nadir durumlarda, eski HTTP İstemcilerinin düzgün şekilde yönlendirilmesi için özel bir durum kodu atamanız gerekebilir. Bu durumlarda, permanent özelliği yerine statusCode özelliğini kullanabilirsiniz, ancak ikisini birden kullanamazsınız. IE11 uyumluluğunu sağlamak için, 308 durum kodu için otomatik olarak bir Refresh başlığı eklenir.
Other Redirects
- API Rotaları ve Rota İşleyicileri içinde, gelen isteğe göre yeniden yönlendirme yapabilirsiniz.
- İçeride
getStaticPropsvegetServerSidePropsile belirli sayfaları istek zamanında yeniden yönlendirebilirsiniz.
Version History
| Version | Changes |
|---|---|
v13.3.0 | missing added. |
v10.2.0 | has added. |
v9.5.0 | redirects added. |