Mapping branch subdomains to folder in master repo

I’ve got a site running at the moment that has a number of micro-sites branching off. This is done by creating a separate branch for each microsite and deploying a branch subdomain as per your feature.

The sites have grown in number to the point that managing the base code common to all the sites is now difficult and I’d like to merge all the existing code into a single repository.

I’d like to move the individual pages into their own folder on the main site and use a redirect to pass through traffic without breaking the existing subdomain mappings.

As of now:
testbranch.coronacorps.com is mapped to a branch subdomain

In future:
testbranch.coronacorps.com/* should map to coronacorps.com/testbranch/:splat

I’ve added a _redirects file to the master branch and checked it ends up in the deployed artifact with the value of https://testbranch.coronacorps.com/* https://coronacorps.com/posts/:splat 301! as a test

That approach did not have the desired effect, I also tried the same approach in the netlify.toml file in the main branch with the same outcome.

I also removed the subdomain branch mapping NETLIFY record and replaced it with a CNAME record and neither of those approaches seemed to change the result.

How do I go about doing this? (domains listed are actual)

Hmm, not sure what you’re asking - at first you say you use branch subdomains, which implies all code is already in one repository, then you’re saying you want to consolidate to one repo? :upside_down_face:

There is no way to do hostname-based redirects (e.g. use a component of the hostname like testbranch in your example) in our server-side redirects. You could do it via client side javascript, since the browser can do more “dynamic” redirects based on information like the hostname.

If I were you, I’d probably architect the migration as I understand it (anything.coronacorps.com/* goes to coronacorps.com/anything) on the client side, Or I’d use a single page app pattern, after assigning all possible hostnames as domain aliases, and handle all requests to all hostnames in your /index.html router, which can be smart enough to show the appropriate content based on the URL without additional clientside redirects, I think (I’m not much of a single page app person but I think this should be possible)

Hmm, not sure what you’re asking - at first you say you use branch subdomains, which implies all code is already in one repository, then you’re saying you want to consolidate to one repo? :upside_down_face:

This is to consolidate all the existing code on many branches of the same repository onto a single branch of that same repository.

There is no way to do hostname-based redirects (e.g. use a component of the hostname like testbranch in your example) in our server-side redirects. You could do it via client side javascript, since the browser can do more “dynamic” redirects based on information like the hostname.

There is no need to extract the hostname dynamically from the request, writing a redirect rule in the netlify.toml file or the _redirects for all possible subdomains is fine.

If I were you, I’d probably architect the migration as I understand it (anything.coronacorps.com/* goes to coronacorps.com/anything) on the client side

My understanding of the documentation of the _redirects and [[redirects]] part of the netlify configuration is that you can redirect subdomain requests onto another arbitrary domain and are able to extract trailing parts of the URL as a :splat parameter.

In that sense, what is stopping me mapping coronacorps.com onto coronacorps.com using your existing static redirection features?

e.g anything.coronacorps.com/* coronacorps.com/anything/:splat 200!

That is precisely what I’ve attempted and have not been able to achieve a result

Given that I wish to deprecate the existing branch to subdomain mappings( and thus remove the NETLIFY records in my DNS) and that I am managing my DNS records primarily through netlify. Will the above configuration be successful if I create CNAME records for those subdomains mapping to my original main netlify provided hostname?

Hope that clears things up

This is to consolidate all the existing code on many branches of the same repository onto a single branch of that same repository.

Thanks! In that case, branch subdomains and deploys are not relevant here.

There is no need to extract the hostname dynamically from the request, writing a redirect rule in the netlify.toml file or the _redirects for all possible subdomains is fine.

oh, cool, then you have a good solution already! I didn’t know how many hostnames we were talking about as you could have 1000 branches, and we cannot support that many hostnames on a site and it would be tedious to write 1000 rules.

My understanding of the documentation of the _redirects and [[redirects]] part of the netlify configuration is that you can redirect subdomain requests onto another arbitrary domain and are able to extract trailing parts of the URL as a :splat parameter.

Kind of. “arbitrary” is the word there that makes me think “no”. A specific, prespecified domain, no problem. “arbitrary” implies “anything matching pattern *.coronacorps.com” to me, which wouldn’t be easily accomplish-able, per my earlier response.

e.g anything.coronacorps.com/* coronacorps.com/anything/:splat 200!

that should work fine, EXCEPT that you forgot the protocol, which makes that invalid (from and to must start with either “https://” or “/”). Try this instead:

https://anything.coronacorps.com/* https://coronacorps.com/anything/:splat 200!

You’ll still need the CNAME records, but they don’t need to change from whatever you have now - they’ll continue to work (for DNS purposes, CNAME to sitename.netlify.com will work just as well as CNAME branchname--sitename.netlify.com).

Hi @fool , following your instructions I have tried the below and still don’t seem to have any luck.

  1. netlify.toml in the master branch of my repository has the following entries
[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200
  
[[redirects]]
  from = "https://secondtestbranch.coronacorps.com"
  to = "https://heuristic-booth-a831e4.netlify.com/posts/a-post.html"
  status = 200
  1. CNAME records created:
Name

secondtestbranch.coronacorps.com
TTL
3600 seconds
Type
CNAME
Value
heuristic-booth-a831e4.netlify.com
  1. No branch is mapped to secondtestbranch.coronacorps.com

  2. dig secondtestbranch.coronacorps.com outputs the following:

; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> secondtestbranch.coronacorps.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52400
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;secondtestbranch.coronacorps.com. IN   A

;; ANSWER SECTION:
secondtestbranch.coronacorps.com. 378 IN CNAME  heuristic-booth-a831e4.netlify.com.
heuristic-booth-a831e4.netlify.com. 20 IN A     142.93.108.123

;; Query time: 60 msec
;; SERVER: 192.168.0.1#53(192.168.0.1)
;; WHEN: Thu Apr 09 00:46:00 BST 2020
;; MSG SIZE  rcvd: 122
  1. nslookup secondtestbranch.coronacorps.com:
Server:  Hitronhub.home
Address:  192.168.0.1

Non-authoritative answer:
Name:    heuristic-booth-a831e4.netlify.com
Addresses:  2a03:b0c0:3:d0::d24:5001
          68.183.215.91
Aliases:  secondtestbranch.coronacorps.com

Running a request from a web browser gives a Not Found response

curl -v https://secondtestbranch.coronacorps.com results in:

 TCP_NODELAY set
 Connected to secondtestbranch.coronacorps.com (134.209.226.211) port 443 (#0)
                                                                                                                                                                                                     Server certificate:                                                                                                                                                                                                                          subject: CN=*.coronacorps.com                                                                                                                                                                                                               start date: Mar 18 10:22:28 2020 GMT                                                                                                                                                                                                        expire date: Jun 16 10:22:28 2020 GMT                                                                                                                                                                                                       subjectAltName: host "secondtestbranch.coronacorps.com" matched cert's "*.coronacorps.com"                                                                                                                                                  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3                                                                                                                                                                                SSL certificate verify ok.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         GET / HTTP/2                                                                                                                                                                                                                               Host: secondtestbranch.coronacorps.com                                                                                                                                                                                                     User-Agent: curl/7.58.0                                                                                                                                                                                                                    > Accept: */*                                                                                                                                                                                                                                >                                                                                                                                                                                                                                                                                                                                                                                                                                           < HTTP/2 404                                                                                                                                                                                                                                 < cache-control: max-age=30, public                                                                                                                                                                                                          < content-length: 9                                                                                                                                                                                                                          < content-type: text/plain; charset=utf-8                                                                                                                                                                                                    < date: Wed, 08 Apr 2020 23:55:45 GMT                                                                                                                                                                                                        < strict-transport-security: max-age=31536000                                                                                                                                                                                                < x-content-type-options: nosniff                                                                                                                                                                                                            < x-frame-options: ALLOWALL                                                                                                                                                                                                                  < x-request-id: 34cb65a4-f9a6-4735-b75a-4275413d3d51
< x-runtime: 0.007079                                                                                                                                                                                                                        < age: 0                                                                                                                                                                                                                                     < server: Netlify                                                                                                                                                                                                                            < x-nf-request-id: 64dd08f6-885c-4ef2-a827-50fc952f3f88-12083334                                                                                                                                                                             <                                                                                                                                                                                                                                            * (304) (IN), TLS Unknown, Unknown (23):                                                                                                                                                                                                     * Connection #0 to host secondtestbranch.coronacorps.com left intact  

This is a 404 response (formatting difficult)

The 404 response has no content, rather than being the netlify default 404 page

I have also run a ‘clear cache and redeploy’ deployment of the master branch.

Build logs show 2 redirect rules in the last deployment.

Please advise on how to get this working?

You’d probably need a force= true on that second redirect to make it work, but as far as I can tell, you have not deployed that branch (secondtestbranch) successfully on that site yet, and we don’t automatically route to the site using the branch subdomain, until you do :slight_smile: