#rewrite from subdomain to subpath
1 messages · Page 1 of 1 (latest)
To achieve the behavior you described, you can use Vercel's rewrite functionality. This will allow you to serve content from mydomain.com/faq when the user visits faq.mydomain.com without changing the URL in the address bar.
Here's how you can configure this in your next.config.js file:
// next.config.js
module.exports = {
async rewrites() {
return {
beforeFiles: [
{
source: '/:path*',
has: [
{
type: 'host',
value: 'faq.mydomain.com',
},
],
destination: '/faq/:path*',
},
],
};
},
};
This configuration ensures that any request to faq.mydomain.com will be rewritten to mydomain.com/faq while keeping the URL in the address bar unchanged.
Alternatively, if you are using a vercel.json file, you can configure the rewrite as follows:
// vercel.json
{
"rewrites": [
{
"source": "/:path*",
"has": [
{
"type": "host",
"value": "faq.mydomain.com"
}
],
"destination": "/faq/:path*"
}
]
}
This setup will achieve the same result, ensuring that requests to faq.mydomain.com are served from mydomain.com/faq without changing the visible URL.
For more details, you can refer to the Vercel documentation on rewrites (https://vercel.com/docs/project-configuration#project-configuration/rewrites).
Sources:
- Vercel Guide on Redirects and Rewrites (https://vercel.com/guides/can-i-redirect-from-a-subdomain-to-a-subpath)
- Vercel Guide on Reverse Proxy Rewrites (https://vercel.com/guides/vercel-reverse-proxy-rewrites-external)