[NEED HELP] redirect user from subdomain

Hi I want a way redirect user from

www.example.com to example.com

How I can do this in frontend canister?

At the moment, it’s not possible to configure redirects in the asset (aka frontend) canister.

However, you can achieve a similar redirect functionality using JavaScript when loading the page:

if (window.location.host === "www.example.com") {
    window.location.replace("https://example.com/");
}

Note: this assumes that the example.com domain is configured among your custom domains for your frontend canister

What’s your end goal, using the same principal on both domains?

Yes but we thought it is easy if we not doing anything by just redirect user from www.example.com to example.com

You can see Openchat did that
https://www.oc.app/ → https://oc.app/ using 307 Internal Redirect

I’m not sure how to implement it in canister, I tried to setup in DNS but it never success

I’m not sure about using a redirect, but personally, I would handle this by instructing II to use the same principal on both domains via the “alternative origin” or “derivation origin” feature as follow.

1. Setup

If you’re using Juno, you can simply set the authentication domain in your juno.config (reference) or through the Console UI.

If you’re setting it up manually, you’ll need to create a file in .well-known containing the list of alternative origins — e.g. your www domain. You can follow the documentation for details.

2. Provide derivation in sign-in

Once the setup is complete, you’ll need to update your sign-in code to include the derivation origin when logging in with II.

With Juno, you can do something like this: https://github.com/peterpeterparker/icdraw/blob/8950c277f19bcf0d54ee6a6c020e71438ff0efc6/src/components/auth/Login.tsx#L16

And with agent-js, like that: oisy-wallet/src/frontend/src/lib/stores/auth.store.ts at c1f4bd98468098f3bfac138ad8e585f3cea27dac · dfinity/oisy-wallet · GitHub

Hope that helps.

3 Likes