# On 404 redirect to another domain with same path

**URL:** <https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [June 21, 2021, 8:17pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652 "2021-06-21T20:17:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex64686](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alex64686/32/10873_2.png) [@alex64686](https://forum.wixstudio.com/u/alex64686)\
**Post date:** [June 21, 2021, 8:17pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/1 "2021-06-21T20:17:37Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [June 21, 2021, 11:45pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/2 "2021-06-21T23:45:23Z")

</div>

Hi Alex,

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

```javascript
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

---

<div class="post-metadata">

**Author:** ![alex64686](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alex64686/32/10873_2.png) [@alex64686](https://forum.wixstudio.com/u/alex64686)\
**Post date:** [June 22, 2021, 2:51am UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/3 "2021-06-22T02:51:38Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [June 22, 2021, 9:10am UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/4 "2021-06-22T09:10:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex64686](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alex64686/32/10873_2.png) [@alex64686](https://forum.wixstudio.com/u/alex64686)\
**Post date:** [June 22, 2021, 3:14pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/5 "2021-06-22T15:14:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [June 22, 2021, 4:00pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/6 "2021-06-22T16:00:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex64686](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alex64686/32/10873_2.png) [@alex64686](https://forum.wixstudio.com/u/alex64686)\
**Post date:** [June 22, 2021, 4:10pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/7 "2021-06-22T16:10:33Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [June 22, 2021, 4:35pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/8 "2021-06-22T16:35:19Z")

</div>

Instead of

```javascript
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:

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

```

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [June 22, 2021, 4:40pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/9 "2021-06-22T16:40:19Z")

</div>

P.S. if you use my code, remove the **/** from my\_domain

---

<div class="post-metadata">

**Author:** ![alex64686](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alex64686/32/10873_2.png) [@alex64686](https://forum.wixstudio.com/u/alex64686)\
**Post date:** [June 22, 2021, 4:49pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/10 "2021-06-22T16:49:46Z")

</div>

@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”]

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [June 22, 2021, 5:09pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/11 "2021-06-22T17:09:04Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [June 22, 2021, 5:13pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/12 "2021-06-22T17:13:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marlowe-shaeffer](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/marlowe-shaeffer/32/669_2.png) [@marlowe-shaeffer](https://forum.wixstudio.com/u/marlowe-shaeffer)\
**Post date:** [June 22, 2021, 6:21pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/13 "2021-06-22T18:21:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alex64686](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alex64686/32/10873_2.png) [@alex64686](https://forum.wixstudio.com/u/alex64686)\
**Post date:** [June 22, 2021, 11:43pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/14 "2021-06-22T23:43:00Z")

</div>

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](http://mywixdomain.com/luisa) it works correctly !! and it redirects me to [myotherdomain.com/luisa](http://myotherdomain.com/luisa).

Then I try again in a new google chrome tab go to [mywixdomain.com/luisa](http://mywixdomain.com/luisa) and this time it redirects me to [myotherdomain.com/error404](http://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.

---

<div class="post-metadata">

**Author:** ![alex64686](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alex64686/32/10873_2.png) [@alex64686](https://forum.wixstudio.com/u/alex64686)\
**Post date:** [June 22, 2021, 11:46pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/15 "2021-06-22T23:46:44Z")

</div>

hi @marlowe-shaeffer ,

the url for the site is [klouser.com](http://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](https://fakedomain.com/fakepath)

the code is:

import wixLocation from ‘wix-location’ ;

$w . onReady (() =\> {  
const my\_domain = ‘[https://fakedomain.com/](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 }` )  
}  
})

---

<div class="post-metadata">

**Author:** ![marlowe-shaeffer](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/marlowe-shaeffer/32/669_2.png) [@marlowe-shaeffer](https://forum.wixstudio.com/u/marlowe-shaeffer)\
**Post date:** [June 22, 2021, 11:51pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/16 "2021-06-22T23:51:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alex64686](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alex64686/32/10873_2.png) [@alex64686](https://forum.wixstudio.com/u/alex64686)\
**Post date:** [June 23, 2021, 1:47am UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/17 "2021-06-23T01:47:35Z")

</div>

thanks all of you for your helping!

@marlowe-shaeffer hope you can solve it!

I stay tuned for news

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [June 24, 2021, 12:30pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/18 "2021-06-24T12:30:19Z")

</div>

@marlowe-shaeffer

Strange, the path of the page, for example, [https://nasriya.net/some-page](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.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [June 24, 2021, 12:55pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/19 "2021-06-24T12:55:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marlowe-shaeffer](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/marlowe-shaeffer/32/669_2.png) [@marlowe-shaeffer](https://forum.wixstudio.com/u/marlowe-shaeffer)\
**Post date:** [June 24, 2021, 3:31pm UTC](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652/20 "2021-06-24T15:31:02Z")

</div>

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!

[Next page](https://forum.wixstudio.com/t/on-404-redirect-to-another-domain-with-same-path/39652.md?page=2)
