-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redirect urls with multiple slashes in the middle of url with status code 301 #2926
Comments
On "Nuxt app (not works): https://stackblitz.com/edit/nuxt-starter-zzxsrh?file=server%2Fmiddleware%2Fredirects.ts" try to put into the address bar a//b//c, and in the server console you will see modified url like a/b/c not a//b//c |
tl;drThe issue is in the httpxy library, specifically in the urlJoin method: https://github.com/unjs/httpxy/blob/main/src/_utils.ts#L195 .replace(/\/+/g, "/") Question for @pi0: What should we do about this? Personally, I think this regexp is either unnecessary or needs rewriting. This only happens in the DEV version, because of Debugging and Pinpointing the IssueI started by looking into what connects Nitro and Nuxt - clearly, it's import { createApp, defineEventHandler } from 'h3';
export const app = createApp();
app.use(defineEventHandler((event) => {
console.log('Original URL:', event.node.req.url);
console.log('New request: ' + event.node.req.originalUrl);
})); Firing up the server and... no bug: 😭 $ pnpm dev:
...
Original URL: /a//b//c
New request: /a//b//c That made me realize the problem must be somewhere in Nitro. So, I looked into how Nitro launches its dev-server. It took a lot of time to go through every line, understand all dependencies and their connections with external packages. I figured out that for the dev-server, Nitro runs the nitro/src/core/dev-server/server.ts Line 304 in 25df63e
Requests are passed down to the worker through the The whole error is caused by the .replace(/\/+/g, "/") This regex processes Ok, it's time to final test: // middleware/test.ts
export default defineEventHandler((event) => {
console.log('[Nitro] Middleware handler - url:', event.node.req.originalUrl);
});
After removing that replace - everything works perfectly! |
@OskarLebuda Great researching! Then it would be logical to give the developer the ability to disable or enable URL normalization |
@talaxasy this issue is only on dev env. Prod env works as expected 😊 |
Let's track in #630 |
Environment
Nuxt config:
Nuxt project info:
Reproduction
Pure Nitro (works): https://stackblitz.com/edit/github-pznd9a?file=server%2Froutes%2F%5B...%5D.ts
Nuxt app (not works): https://stackblitz.com/edit/nuxt-starter-zzxsrh?file=server%2Fmiddleware%2Fredirects.ts
Describe the bug
I'm trying to create middleware that will redirect urls with multiple slashes in the middle of url with status code 301.
I created such middleware (src/middleware/01.trailing-slashes.global.ts):
But to.path already comes normalized, without slashes.
For example I make req on "http://localhost:3000/produkte////atemwege/prod1" and in middleware get a "/produkte/atemwege/prod1" as you can see I already get to.path without extra slashes.
I even tried with server middleware (src/server/middleware/redirects.ts):
But event.node.req.ur or event.node.req.originalUrl comes without slashes.
What I want to achieve is I just need to make a redirect for such cases:
http://localhost:3000/a/////b/c (301) -> http://localhost:3000/a/b/c (200)
http://localhost:3000///a///b///c (301) -> http://localhost:3000/a/b/c (200)
What is the current behavior:
http://localhost:3000/a/////b/c (200) -> http://localhost:3000/a/b/c (200)
http://localhost:3000///a///b///c (200) -> http://localhost:3000/a/b/c (200)
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: