簡體   English   中英

Next.js 301:如何從有查詢的源重定向到沒有查詢的目標

[英]Next.js 301: How to redirect from source with query to destination without query

如果有人幫助我,我將不勝感激:

  1. 我有一個來源 URL 像https://example.com/category/results/refine?dealers=123&inv=true&sellertypes=dealer
  2. 如果源 URL 包含dealersinv密鑰,我想實施 SSR 永久 301 重定向到目標 URL,例如https://example.com/
  3. 我在我的next.config.ts中實現了以下代碼:

 async redirects() { return [ { source: "/category/results/refine", has: [ { type: "query", key: "dealers", }, { type: "query", key: "inv", value: "true", }, ], destination: "/", permanent: true, statusCode: 301, }, }

  1. 結果,我得到的重定向不是https://example.com/而是https://example.com/?dealers=123&inv=true&sellertypes=dealer 我的意思是,來自源 URL 的查詢堅持到目標 URL。這不是我所期望的。

看起來這不能通過nextjs配置來實現——文檔指出:

應用重定向時,請求中提供的任何查詢值都將傳遞到重定向目標。 例如,看下面的重定向配置:當請求/old-blog/post-1?hello=world時,客戶端會被重定向到/blog/post-1?hello=world。

你有兩個選擇。

  1. 實現自定義服務器,然后進行路徑匹配和刪除查詢參數的代碼

  2. 實施代理,如 nginx 或 CDN,為您提供精細的路由控制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM