On 404 redirect to another domain with same path

Hi everyone!

I want to redirect to another domain when 404 occurs.

For example if the user go to my wix site mydomain.com /example and it’s not a wix page so redirect to mydomain.app/example

there is a way to do this?

I created the 404 custom page but i didn’t find a way to obtain the previus url when we i’m in 404
page.

I red about routers but i can’t achieve the my goal. Maybe you can share a tip to do it.

thanks!!

1 Like

Hi Alex,

Yes, you can do that, on the 404 page, write down this code:

import wixLocation from 'wix-location';

$w.onReady(() => {
    const my_domain = 'https://nasriya.net/'; // The other domain
    const path = wixLocation.path || []; // The current location data
    
    if (path.length > 0) {
        let path = ''; // The current stringified path
        path.forEach(directory => {
            path = `${path}/${directory}`;
        });
        
        // Redirect the visitor to the second domain
        wixLocation.to(`${my_domain}${path}`)
    }
})

When you land on the 404 page, the code will take the path from the curent location, build the path from the array and redirect you to the the second domain with the same path.

Note: There might be a processing delay, so you’re advised to use a preloader on the page untill the procesing is done.

Hope this helps~!
Ahmad

@ahmadnasriya , thanks for helping!

Ohh yes but when call wixLocation.path, the path is “error404” (the custom page for 404) and i need the path for the previuos page of 404

Let me check the router solution, I’ll update you soon.

thanks for helping @ahmadnasriya . I hope you find a solution! I have been trying for a long time and I have not succeeded.

No. The path is the wrong page path (at least for me).

@J.D. the solution above doesn’t work, I was just thanking you for trying to help me. I’m still waiting to see if you find a solution.

Sorry for the confusing message.

Instead of

if (path.length > 0) {
        let path = ''; // The current stringified path
        path.forEach(directory => {
            path = `${path}/${directory}`;
        });
        
        // Redirect the visitor to the second domain
        wixLocation.to(`${my_domain}${path}`)
    }

Write:

if (path.length > 0) {
        let pathString = path.join("/");
        wixLocation.to(`${my_domain}/${pathString }`)
    }

P.S. if you use my code, remove the / from my_domain

@J.D yes but in my case pathString is “error404” and i want to go to the previuos url.

E.g, the user goes to mywixsite.com/{ dynamic_path} and this url dosn’t exist so i want to redirect the user to mycustomdomain.com/ { dynamic_path} . The problem is that wix return the user first to error404 page and when i try to get the path { dynamic_path} to do the redirect, the response for wixLocation.path is [“error404”]

It does not happen to me (I get the expected path, not the “error404”), so I can’t help… It’s strange that we both see different behavior…

Ah, I know why the difference. You have for some reason the old version (bolt). I don’t know why.

Hi Alex,
Can you please share the URL for your site, and include details about where the issue is (page/code), so we can investigate? Thanks.

The situation gets weirder:

I deleted and re-created the custom 404 error page and inserted the above code into the onReady.

Then I publish the site and ONLY THE FIRST TIME I try to go to: mywixdomain.com/luisa it works correctly !! and it redirects me to myotherdomain.com/luisa.

Then I try again in a new google chrome tab go to mywixdomain.com/luisa and this time it redirects me to myotherdomain.com/error404 and it doesn’t work anymore.

I try in an incognito tab and it keeps redirecting to / error404

Then I open Mozilla firefox and the same behavior happens: The first time it works and then it doesn’t.

hi @marlowe-shaeffer ,

the url for the site is klouser.com

the page is “error404” and the code is in “onReady” function.

you can test now going to klouser.com/fakepath and check if redirect you to https://fakedomain.com/fakepath

the code is:

import wixLocation from ‘wix-location’ ;

$w . onReady (() => {
const my_domain = ‘https://fakedomain.com/’ ; // The other domain
const path = wixLocation . path || ; // The current location data

if ( path . length > 0 ) {
let pathString = path . join ( “/” );
wixLocation . to ( ${ my_domain }${ pathString } )
}
})

Thanks! As J. D. said, your site is using our old rendering engine, which is likely why these issues are happening. I’ve sent the info to our QA team to investigate, and hopefully we can move you over to our new rendering engine. I’ll have more info for you tomorrow.

thanks all of you for your helping!

@marlowe-shaeffer hope you can solve it!

I stay tuned for news

@marlowe-shaeffer

Strange, the path of the page, for example, https://nasriya.net/some-page doesn’t exist, you visit this page, you’ll get a 404 error as expected, but when reading the path by code, the path is /error404 , which is not what looks on the actual path, this has always been the case.

Ahmad, as I said, with Bolt you get “error404” as page path, but with Thunderbolt you get the “/some-page” as page path.

Hi Alex,
We’ve moved your site to the new rendering engine, so this issue should be resolved. Let me know if everything is OK now. Thanks!