Rewrites to external URL not working for me
Answered
Gabriel Moresco posted this in #help-forum
Hello guys, I have a system made in React Native Web (https://app.favorito.digital) using Expo, and my website in NextJS (https://favorito.digital).
I want to redirect my website users when entering the
I did the following rewrite in
But when entering the
I want to redirect my website users when entering the
/app path to my React Native system.I did the following rewrite in
next.config.js./** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/app',
destination: 'https://app.favorito.digital',
},
]
},
}
module.exports = nextConfigBut when entering the
/app path, an error occurs when directing. This rewrites is on my production website, so you can test it.Answered by joulev
How to fix: also rewrite the assets required to load the page, or use absolute url (with the domain) inside your app.domain.com static asset URLs
49 Replies
Common paper wasp
I also need that, please help
Australian Freshwater Crocodile
Rewrites can't be used for that. Docs:
You want to use redirects, altough im not 100% sure it will work for external URLS
https://nextjs.org/docs/app/api-reference/next-config-js/redirects
Rewrites act as a URL proxy and mask the destination path, making it appear the user hasn't changed their location on the site. In contrast, redirects will reroute to a new page and show the URL changes.You want to use redirects, altough im not 100% sure it will work for external URLS
https://nextjs.org/docs/app/api-reference/next-config-js/redirects
I did the same way, but is not working
Australian Freshwater Crocodile
link?
Australian Freshwater Crocodile
Ok, sorry about the misinformation.
I had a look in yourwebsite. The problem seems to be that your redirect is owrking fine. but then the website is looking for other resources like
To fix this, I suggest changing from a rewrite to a redirect. Alternatively. you can add more rewrites for these paths... but this can get messy.
I had a look in yourwebsite. The problem seems to be that your redirect is owrking fine. but then the website is looking for other resources like
https://favorito.digital/static/js/663.faae7576.js and cannot find them there. They are available on https://app.favorito.digital/static/js/663.faae7576.jsTo fix this, I suggest changing from a rewrite to a redirect. Alternatively. you can add more rewrites for these paths... but this can get messy.
you could try with
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/app',
destination: 'https://app.favorito.digital',
},
{
source: '/app/:path*',
destination: 'https://app.favorito.digital/:path*',
},
]
},
}
module.exports = nextConfigI already tried that, but when i add the
/app/somethinhere, next move to not found pagei recorded a video here
@Australian Freshwater Crocodile Ok, sorry about the misinformation.
I had a look in yourwebsite. The problem seems to be that your redirect is owrking fine. but then the website is looking for other resources like `https://favorito.digital/static/js/663.faae7576.js` and cannot find them there. They are available on https://app.favorito.digital/static/js/663.faae7576.js
To fix this, I suggest changing from a rewrite to a redirect. Alternatively. you can add more rewrites for these paths... but this can get messy.
It's a possibility, but I'm trying to solve it with rewrite to improve my website backlinks for SEO purposes
The
app.favorito.digital/somethinghere link is added to most of our customers, so if i could change to favorito.digital/app/somethinghere i think my SEO backlinks will improve@Gabriel Moresco I already tried that, but when i add the `/app/somethinhere`, next move to not found page
Australian Freshwater Crocodile
Can you show the redirect you are using. Can you try with a path you know exists? Like the one I posted above?
@Australian Freshwater Crocodile you could try with
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/app',
destination: 'https://app.favorito.digital',
},
{
source: '/app/:path*',
destination: 'https://app.favorito.digital/:path*',
},
]
},
}
module.exports = nextConfig
i'm using the same that you suggested here
this is a real example
Australian Freshwater Crocodile
You can see something else is in play on that video, you are being reidrected from
/app/nordenbleau to /nordenbleau Could it be that on the real next config you have another redirect?/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/app',
destination: 'https://app.favorito.digital',
},
{
source: '/app/:path*',
destination: 'https://app.favorito.digital/:path*',
},
]
},
}
module.exports = nextConfigthis is my entire
next.config.jsAustralian Freshwater Crocodile
as a test, can you try with a more specific path?
this will test if the issue is with the path matching, or if there is something else at play
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/app',
destination: 'https://app.favorito.digital',
},
{
source: '/app/nordenbleau',
destination: 'https://app.favorito.digital/nordenbleau',
},
]
},
}
module.exports = nextConfigthis will test if the issue is with the path matching, or if there is something else at play
same behavior, redirecting to not found page 😦
sorry!
Australian Freshwater Crocodile
is this also happening in a dev environemnt?
could it be you have a redirect in place with the name service? This is not my area of expertise... but it could be the case
could it be you have a redirect in place with the name service? This is not my area of expertise... but it could be the case
@Gabriel Moresco Hello guys, I have a system made in React Native Web (https://app.favorito.digital) using Expo, and my website in NextJS (https://favorito.digital).
I want to redirect my website users when entering the `/app` path to my React Native system.
I did the following rewrite in `next.config.js`.
js
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/app',
destination: 'https://app.favorito.digital',
},
]
},
}
module.exports = nextConfig
But when entering the `/app` path, an error occurs when directing. This rewrites is on my production website, so you can test it.
This is likely the cause:
Your app.favorito.digital uses static assets at, say, /script.js. So app.domain.com/script.js works.
But the /script.js route is not rewritten, hence the browser makes the request to domain.com/script.js instead, which 404’s and make your app not work
Your app.favorito.digital uses static assets at, say, /script.js. So app.domain.com/script.js works.
But the /script.js route is not rewritten, hence the browser makes the request to domain.com/script.js instead, which 404’s and make your app not work
How to fix: also rewrite the assets required to load the page, or use absolute url (with the domain) inside your app.domain.com static asset URLs
Answer
hummm, i will try to do that, thanks
@joulev How to fix: also rewrite the assets required to load the page, or use absolute url (with the domain) inside your app.domain.com static asset URLs
what do you mean with
or use absolute url (with the domain) inside your app.domain.com static asset URLs?@Gabriel Moresco what do you mean with `or use absolute url (with the domain) inside your app.domain.com static asset URLs`?
Instead of using <script src='/script.js'> in your app.domain.com, use <script src='https://app.domain.com/script.js'>
How to do that depends on how you built your app.domain.com and it may not be easy
I use React Native Web with Expo, i will search how to do that
I added two new rewrites and worked
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/manifest.json',
destination: 'https://app.favorito.digital/manifest.json',
},
{
source: '/static/:path*',
destination: 'https://app.favorito.digital/static/:path*',
},
{
source: '/app',
destination: 'https://app.favorito.digital',
},
{
source: '/app/:path*',
destination: 'https://app.favorito.digital/:path*',
},
]
},
}
module.exports = nextConfig/manifest.json and /static/:path*but when trying to go to
/app/somethinghere redirects to not found pageany idea why?
i recorded the case
{
source: '/app/:path*',
destination: 'https://app.favorito.digital/:path*',
},i had this rewrite
interesting....
i can reproduce that too
@Gabriel Moresco any idea why?
ok cause found, this is a pretty nasty bug.
*
* rewritten to
* redirected to
* browser makes request to
*
/app/somethinghere* rewritten to
https://app.domain.com/somethinghere but the domain remains domain.com* redirected to
/somethinghere/ because of the trailing slash handling* browser makes request to
domain.com/somethinghere/ which 404you need to handle the trailing slash explicitly too
i suppose you can do it in either the nextjs side or the expo app side
@joulev you need to handle the trailing slash explicitly too
I tried that, but not works, can you help me?
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/manifest.json',
destination: 'https://app.favorito.digital/manifest.json',
},
{
source: '/static/:path*',
destination: 'https://app.favorito.digital/static/:path*',
},
{
source: '/app',
destination: 'https://app.favorito.digital',
},
{
source: '/app/:path*',
destination: 'https://app.favorito.digital/:path*',
},
{
source: '/app/',
destination: 'https://app.favorito.digital/',
},
{
source: '/app/:path*/',
destination: 'https://app.favorito.digital/:path*/',
},
]
},
}
module.exports = nextConfig@Gabriel Moresco I tried that, but not works, can you help me?
js
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/manifest.json',
destination: 'https://app.favorito.digital/manifest.json',
},
{
source: '/static/:path*',
destination: 'https://app.favorito.digital/static/:path*',
},
{
source: '/app',
destination: 'https://app.favorito.digital',
},
{
source: '/app/:path*',
destination: 'https://app.favorito.digital/:path*',
},
{
source: '/app/',
destination: 'https://app.favorito.digital/',
},
{
source: '/app/:path*/',
destination: 'https://app.favorito.digital/:path*/',
},
]
},
}
module.exports = nextConfig
try
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/manifest.json',
destination: 'https://app.favorito.digital/manifest.json',
},
{
source: '/static/:path*',
destination: 'https://app.favorito.digital/static/:path*',
},
{
source: '/app',
destination: 'https://app.favorito.digital',
},
{
source: '/app/:path*',
destination: 'https://app.favorito.digital/:path*/',
},
]
},
}
module.exports = nextConfig;note: depending on how you build
app.favorito.digital, this may stop working in the future if the way that app handles trailing slash changes. but as of now, this works wellSolved ✅ðŸ™ðŸ»
Thank you so much @joulev