Redirecting a subdomain to an external URL with Netlify DNS
I’m waiting in the STL airport after an eventful week meeting my new team QuarkWorks, Inc. for the first time. I have some time before my flight home, so I figured I’d chip away at the blog.
One minor gripe I’ve had with my test drive of Ghost CMS is remembering how to
access the admin dashboard where I write posts. I’ve deployed ghost to a
subdomain of my site at ghost.mattrossman.com
, which means my admin panel is
hosted at ghost.mattrossman.com/ghost
. I would prefer to access the page via
admin.mattrossman.com
so that I only have to type the first few characters to
trigger browser autocomplete. Here’s how I set up that redirect.
My root domain mattrossman.com
is managed by Netlify DNS. I was hoping I could
simply configure a new “record” in the domain panel to create this redirect, but
turns out it’s not that simple. The steps are actually:
- Create a new app on Netlify
- In a new folder, write a
_redirects
file with the desired redirect rule and an emptyindex.html
- Deploy manually by drag + dropping that folder
- Configure your desired subdomain as the custom domain for that app
The tricky part here was what to put in _redirects
. I saw several sources
saying that you could just write something like this to perform a catch-all
redirect:
/* https://example.com/whatever
However, this doesn’t work. Whenever I visit the root of my new app, it strips
away the leading slash and the redirect doesn’t complete. I also tried removing
the slash from that redirects file leaving just the *
but that didn’t work
either. It still only performed the redirect if I typed something after the
slash in my browser.
I found the solution here. You can force the redirect by specifying the desired status code with an exclamation point.
/* https://example.com/whatever 301!
I got a warning when I tried configuring the custom subdomain which made me wonder if it would cause a configuration conflict with my root domain. Rest assured, I ignored the message and my root domain still works properly.