So the only solution I found so far isn't perfect.
Using the redirect-module , I added the following at the top of my redirects list:
{
// eslint-disable-next-line
from: '(?!^\/$|^\/[?].*$)(.*\/[?](.*)$|.*\/$)',
to: (from, req) => {
const base = req._parsedUrl.pathname.replace(/\/$/, '');
const search = req._parsedUrl.search;
return base + (search != null ? search : '');
}
},
Also in nuxt.config.js I made sure to add the trailing slash configuration. (See the doc)
router: {
trailingSlash: false
},
Note:
It redirect all URLS ending with '/' with query parameters, but it doesn't match the home page '/' (which seems to be handled somehow)
The following will redirect the following:
'/blog/' to '/blog'
'/blog/?a=b' to '/blog?a=b'
'/blog/foo/' to '/blog/foo'
'/blog/foo/?a=b' to '/blog/foo?a=b'
'/' to '/'. --> won't work
'/?a=b' to '/?a=b'. --> won't work
I made a test list available here
Explanation of the regex
It might not be perfect since I'm not a Regex expert but:
'(?!^\/$|^\/[?].*$)(.*\/[?](.*)$|.*\/$)'
It's broken it 2 pieces: 1 exclusion, and 1 inclusion.
The exclusion: (?!^\/$|^\/[?].*$), consist of a check to exclude standalone trailing comma (/) or standalone trailing comma with query string /?foo=bar routes. It's used mainly for the home page.
The inclusion: (.*\/[?](.*)$|.*\/$), consists of checking for the trialing comma (/blog/) or a trailing comma with query string (/blog/?foo=bar)