Customize Routing Rule for Main domain and Subdomain(wildcard)
I have created a file
app/router.options.ts
for the subdomain
Project Structure
In nuxt config
Above, how to customize the route rules for a subdomain3 Replies
Hi @vrijraj. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To customize route rules for subdomains (including wildcards) in Nuxt, you should use the routeRules feature in your nuxt.config. However, Nuxt’s routeRules are matched against the route path, not the domain or subdomain. This means you cannot directly specify a route rule for a subdomain (like
You cannot directly customize routeRules for subdomains using nuxt.config. Route rules only match paths. For subdomain-specific behavior, use custom server middleware or logic in your router options to inspect the host and apply the desired behavior [Custom Routing]. If you need more advanced host-based routing, consider using a reverse proxy (like Nginx) in front of your Nuxt app to route requests to different Nuxt instances or endpoints based on the subdomain. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
*.example.com
) using only the built-in routeRules configuration. Route rules are designed to match URL paths (e.g., /about
, /subdomain/**
), not hostnames or subdomains [routeRules API].
Key points from the knowledge sources:
- routeRules are global options applied to matching server routes, and they match by path, not by domain or subdomain [routeRules API].
- If you need to apply different logic based on the subdomain, you must handle this at runtime (e.g., in middleware, server routes, or custom router logic), not via routeRules in nuxt.config.
- Your approach of customizing routes in app/router.options.ts
is valid for changing which routes are available based on the subdomain, but it does not affect routeRules, which are still path-based [Custom Routing].
What you can do:
- You can continue to use your custom router logic in app/router.options.ts
to serve different routes for subdomains.
- For routeRules, you can only target the path (e.g., /subdomain/**
), not the subdomain itself.
- If you need to apply SSR/prerender/caching rules based on the subdomain, you would need to implement this logic in server middleware or in your API/server routes, where you have access to the request headers and can inspect the host.
Example:
This will apply SSR to all /subdomain/**
paths, regardless of the domain or subdomain.
If you need true subdomain-based rules:
You must implement custom logic in server middleware or API routes, where you can check the Host
header and apply your own logic accordingly. Nuxt’s routeRules do not support host-based matching as of the current documentation [routeRules API].
Summary:You cannot directly customize routeRules for subdomains using nuxt.config. Route rules only match paths. For subdomain-specific behavior, use custom server middleware or logic in your router options to inspect the host and apply the desired behavior [Custom Routing]. If you need more advanced host-based routing, consider using a reverse proxy (like Nginx) in front of your Nuxt app to route requests to different Nuxt instances or endpoints based on the subdomain. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@600276141252673536> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
not working