# Questions about multi-tenant setup

**URL:** <https://answers.netlify.com/t/questions-about-multi-tenant-setup/109009>\
**Category:** Support\
**Tags:** redirects, netlify-dns-https-ssl\
**Created:** [December 21, 2023, 8:13am UTC](https://answers.netlify.com/t/questions-about-multi-tenant-setup/109009 "2023-12-21T08:13:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alireza](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/alireza/32/57430_2.png) [@Alireza](https://answers.netlify.com/u/Alireza)\
**Post date:** [December 21, 2023, 8:13am UTC](https://answers.netlify.com/t/questions-about-multi-tenant-setup/109009/1 "2023-12-21T08:13:21Z")

</div>

Hi! I am considering moving from Vercel to Netlify because it looks like you have better support for multi-tenant apps. Reading other threads on wildcard subdomains, I know that it needs to be a Pro+ team account.

Appreciate it if you could help with my questions before migration.

**Question 1:**  
Assuming my primary domain is `site.dev`. I can **only** use `*.site.dev` for wildcard subdomains and it is not possible to have `*.users.site.dev`. Is that correct?

**Question2:**  
I already have some other subdomains in use. They are set up using CNAME records. For example `auth.site.dev` has a CNAME to my Firebase auth, or `12242.site.dev` has a CNAME to sendgrid.  
My understanding is that I need to add either a CNAME with `*` to my Netlify site address or I can add CNAMEs for subdomains individually (like a CNAME for `steve.site.dev` and another CNAME for `ben.site.dev` both pointing to my Netlify site address.).

To be clear let me ask my question with an example. Assuming there are these 2 DNS records on my domain (`site.dev`). They are used to set wildcard subdomains, and also to set custom auth service:

- CNAME: `*` → `mysite.netlify.app`
- CNAME: `auth` → `custom.auth.provider`

Am I correct to assume that if you go to `https://auth.site.dev`, the request will go to `custom.auth.provider`? I am assuming that the specific value (`auth` in this case) will take precedence on the wildcard (`*`).

**Question 3:**  
As for the multi-tenant app. The app is build using Nuxtjs. Each user gets a path (`/profile/<username>`). After setting up the wildcard subdomains, I want to rewrite `https://<username>.site.dev/*` with `https://site.dev/profile/<username>/*`. Is that something that can be achieved using netlify.toml or you suggest an edge function for it?

Thanks a lot for your time.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [December 24, 2023, 2:05pm UTC](https://answers.netlify.com/t/questions-about-multi-tenant-setup/109009/2 "2023-12-24T14:05:47Z")

</div>

> [@Alireza](#):
>
> Assuming my primary domain is `site.dev`. I can **only** use `*.site.dev` for wildcard subdomains and it is not possible to have `*.users.site.dev`. Is that correct?

Technically, you’d need to have the domain `www.site.dev` or basically `<anything>.site.dev` for `*.site.dev` to work on that site. But yes, you can’t have `*.users.site.dev` on the same site, you can do that on a different one.

> [@Alireza](#):
>
> Am I correct to assume that if you go to `https://auth.site.dev`, the request will go to `custom.auth.provider`? I am assuming that the specific value (`auth` in this case) will take precedence on the wildcard (`*`).

Yes, and that has nothing to do with Netlify, but that’s just have DNS will work. Specific entries will take preference over wildcards.

> [@Alireza](#):
>
> Is that something that can be achieved using netlify.toml or you suggest an edge function for it?

You’d have to use Edge Functions or even a middleware in Nuxt would work. Nuxt can be deployed to Edge Functions, so no need to write a separate Edge Function just to handle this redirect, unless you need Nuxt to be deployed to AWS Lambda due to whatever reason.

---

<div class="post-metadata">

**Author:** ![Alireza](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/alireza/32/57430_2.png) [@Alireza](https://answers.netlify.com/u/Alireza)\
**Post date:** [December 24, 2023, 3:27pm UTC](https://answers.netlify.com/t/questions-about-multi-tenant-setup/109009/3 "2023-12-24T15:27:56Z")

</div>

Thank you @hrishikesh for taking the time and for your helpful answer 🙂

On the 3rd question, my requirement is to rewrite (not redirect) `<username>.site.dev` to `www.site.dev/profile/<username>`, to be clear, when a user goes to `john.side.dev/*` the URL won’t change but the served response will the same as `www.site.dev/profile/john/*`. That way each user can set a CNAME on their domain pointing to our subdomain in order to have their profile and subpaths under their domain.

For example: when you go to `johndoe.com`, while your browser url is showing `https://johndoe.com` you will see the contents that is also available at `https://www.site.dev/profile/john/`.

Hope it makes sense and looks like it is possible to achieve with Netlify.

What I appreciate help with is understanding what the Edge Function workflow would look like.

I understand that [a rewrite is possible](https://edge-functions-examples.netlify.app/example/rewrite). And I also understand that [rewrite is the same as a redirect but with status code 200](https://docs.netlify.com/routing/redirects/). Is there an example of how to setup with rewrite from subdomain to path using edge functions?

Looks like my question is very similar to [this one](https://answers.netlify.com/t/redirect-wildcard-subdomain-to-a-path/42637), I hope things have changed since 2021.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [December 24, 2023, 5:19pm UTC](https://answers.netlify.com/t/questions-about-multi-tenant-setup/109009/4 "2023-12-24T17:19:08Z")

</div>

> [@Alireza](#):
>
> Is there an example of how to setup with rewrite from subdomain to path using edge functions?

Untested, but something like this:

```auto
export default async (req : Request) => {
  const reqUrl = new URL(req.url)
  return new URL(`/profile/${reqUrl.hostname.split('.')[0]}/${reqUrl.pathname}`, 'https://www.site.dev/')
}

```

_should_ work for a URL like: `https://john.com/` that needs to be rewritten to `https://www.site.dev/profile/john/`. This would further take into account the path on the original URL, for example: `https://john.com/foo/` would now be rewrriten to `https://www.site.dev/profile/john/foo/`. This **would not** work as intended for `https://www.john.com` due to the `www` in the hostname. It would require some special handling.

---

<div class="post-metadata">

**Author:** ![Alireza](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/alireza/32/57430_2.png) [@Alireza](https://answers.netlify.com/u/Alireza)\
**Post date:** [December 24, 2023, 9:44pm UTC](https://answers.netlify.com/t/questions-about-multi-tenant-setup/109009/5 "2023-12-24T21:44:17Z")

</div>

> [@hrishikesh](#):
>
> ```auto
> export default async (req : Request) => {
> const reqUrl = new URL(req.url)
> return new URL(`/profile/${reqUrl.hostname.split('.')[0]}/${reqUrl.pathname}`, 'https://www.site.dev/')
> }
> 
> ```

That’s a great response @hrishikesh. I know have a better understanding.

Now my next quest is to find out how to return a URL as the response using a Nitro middleware (Nuxt server middleware). My idea is to have a Nuxt server middleware (`~/server/middleware/subdomain.ts`). This middleware will be executed whenever a request hits any server route. In side I can do the checks and send the new URL.

The thing is I don’t know how to do that at the moment.

---

<div class="post-metadata">

**Author:** ![Alireza](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/alireza/32/57430_2.png) [@Alireza](https://answers.netlify.com/u/Alireza)\
**Post date:** [December 24, 2023, 11:44pm UTC](https://answers.netlify.com/t/questions-about-multi-tenant-setup/109009/6 "2023-12-24T23:44:14Z")

</div>

Ah! scratch that!

Nuxt server middlewares are not supposed to return a response.

> **[server/ · Nuxt Directory Structure](https://nuxt.com/docs/guide/directory-structure/server#server-middleware)**
>
> The server/ directory is used to register API and server handlers to your application.

> Middleware handlers should not return anything (nor close or respond to the request) and only inspect or extend the request context or throw an error.

So, that’s not possible with a Nuxt middleware to check if the request is coming from a subdomain and rewrite it to a path.

Using a Netlify Edge function looks pretty straightforward though. I think I just need to add a function that runs on every request to server, checks the request host, and if it is a subdomain, rewrite it to a path, by returning a new URL. Is that right?

I don’t know how to do that in Nuxt, though.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [December 27, 2023, 1:20pm UTC](https://answers.netlify.com/t/questions-about-multi-tenant-setup/109009/7 "2023-12-27T13:20:59Z")

</div>

Yeah it appers that Nuxt doesn’t really support rewrites. You would have to rely on native Netlify Edge Functions then.
