Replies: 2 comments
|
I've been facing this issue, event |
|
Hi @vibdev95, this issue occurs because Next.js performs URL normalization at the server routing layer before your custom Since Solution 1: Catching double slashes at the Reverse Proxy level (Recommended)If you are running Next.js behind Nginx, Cloudflare, AWS CloudFront, or Varnish, you should catch double slashes before the request reaches the Next.js Node.js server process and return a Example for Nginx: # Catch URLs with double slashes in the request path and return 404
if ($request_uri ~ "^(?<clean_path>/[^?]*?)//+(?<rest>.*)$") {
return 404;
}
Solution 2: Explicit Custom Redirection or Rewrite via next.config.js
If you cannot use a reverse proxy, you can attempt to intercept the pattern directly in next.config.js using regex rewrites/redirects before the internal normalizer triggers:
JavaScript
// next.config.js
module.exports = {
// Disables automatic trailing slash handling if needed
trailingSlash: false,
async redirects() {
return [
// Attempt to capture double slashes and handle them explicitly
{
source: '/:path*//:rest*',
destination: '/404',
permanent: false, // Or true, depending on requirements
},
]
},
}
Summary for SEO
From an SEO perspective, Google and other search engine bots consider a 308 redirect as a standard canonical redirection. However, if you strictly want to prevent redirect chains or stop crawlers from indexing // routes, blocking them at the edge/CDN level (Solution 1) is the cleanest approach.
If this explanation helps you or resolves your issue, please consider marking this response as the accepted answer so it can help others in the community! 🟢 |
Uh oh!
There was an error while loading. Please reload this page.
Summary
I am reaching out to discuss an issue I've encountered regarding URL redirection handling within Next.js, which is impacting the SEO performance of our pages.
The problem arises when there are URLs containing double slashes ('//') in between. Currently, Next.js automatically redirects such URLs to the corresponding URL without the double slash. However, this behavior is causing a significant increase in the count of redirects, adversely affecting our SEO efforts.
I attempted to address this issue by implementing middleware to detect and handle requests with double slashes by returning a 404 error. However, it appears that the redirection with a status code 308 is executed before the request even reaches the middleware.
Has anyone also faced this issue? were you able to fix this?
I found this was earlier reported as an issue too, link to the issue But, looks like this was closed automatically.
Additional information
No response
Example
No response
All reactions