0

I have several url's that I would like to rewrite in NGINX for example:

From: app.example.com/calendar
To: calendar.example.com

Or

From: app.example.com/meetings
To: meetings.example.com

I would still like to keep the app.example.com so it's not being removed from the redirect, but just create subdomains for certain URLs.

How can I do this in NGINX conf file? All the best.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Mensur
  • 457
  • 9
  • 28

1 Answers1

0

Redirect Subdomain to Folder in NGINX

Just add the following location block in your server configuration, inside server block, above the location / block.

location ^~ /calendar {
    rewrite ^/calendar/?(.*)$ https://calendar.example.com/$1 permanent;
}

In the above code replace blog.example.com with your subdomain, and /calendar with your subdirectory. The above location block will listen to all requests sent to example.com/calendar. It redirects all those requests to calendar.example.com subdomain of your website. $1 is the URL stub after subfolder in requested URL. We add permanent keyword at the end of rewrite directive to cause a permanent redirect.

So it will permanently redirect subfolder to subdomain along with URL string.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Jimmy
  • 1
  • 1
  • 3
  • 1
    The OP is not wanting an _external_ redirect, like they say in the question: "I would still like to keep the `app.example.com` so it's not being removed from the redirect" and they have tagged the question `nginx-reverse-proxy`. (Where did "blog" come from?) – MrWhite Feb 12 '23 at 11:19