Netlify Custom Domain and WordPress

Updated question

I have a WordPress site set up on Server A.

Using Gatsby and gatsby-source-wordpress, a plugin that calls the WordPress API and pulls in blog posts, I am dynamically assembling pages and deploying a Gatsby site to Netlify.

I have set up a custom domain on Netlify and changed my custom domain’s nameservers to point to Netlify.

The problem is when I navigate to mycustomdomain.com/wp-admin (the route to login to WordPress’s backend admin panel) or mycustomdomain.com/wp-json (the API endpoint to access post content), the URLs are redirected to Netlify instead of Server A, where my WordPress installation lives, because the nameservers for the custom domain are set up to point to Netlify.

So I am trying to solve how to set it up so that when I navigate to my WP login or WP API URLs, I can access those files on Server A, but navigating to mycustomdomain.com goes to my Gatsby/Netlify build.

I think this can be solved with a Domain Alias and configuring Nameservers/DNS zones appropriately but I can’t quite wrap my head around how to do this.

Thanks,

I see this post was withdrawn. Let us know if you still have questions @songfarm-david.

Hi @futuregerald, I actually am still trying to resolve this.

Thanks,

Hi @songfarm-david, sorry you’ve been waiting so long for a reply!

What I would do in your position is create a subdomain to point to Server A. Since you’ve pointed your domain nameservers to Netlify, that should mean that you’re using Netlify DNS management, which you should find under the Domains tab at the top of your team site list.

Add a new record with your chosen subdomain in the Name field (replacing the @). For example, you could enter wp to get the subdomain wp.yourcustomdomain.com. Point that record to Server A (using whatever DNS settings that server requires).

Once you do that, after DNS propagates, you should be able to go to yoursubdomain.yourcustomdomain.com/wp-admin and access your WP admin.

If you really want to get fancy, you can add a couple of proxy redirects so it looks like you’re working in one seamless site. (This can also avoid potential CORS issues if you’re accessing your WP API using JavaScript.)

To do this, create a file called _redirects file in the base of your published site. Add two lines to that file:

/wp-admin     https://yoursub.yourdomain.com/wp-admin    200
/wp-json      https://yoursub.yourdomain.com/wp-json     200

With that in place, you can go to yourdomain.com/wp-admin and access your WP admin as if it’s part of the same site. Something extra nice about this is that you can change the first /wp-admin path to anything you want, for example:

/doorbell     https://yoursub.yourdomain.com/wp-admin    200

Then you’d go to yourdomain.com/doorbell to access your admin. You’d be amazed how many requests our servers get for wp-admin paths on our customers’ sites, even though we can’t even host WP sites! Hackers just keep trying anyway. Changing your admin URL just makes things one step harder for someone to hack your WP admin.

Hope that helps!

3 Likes

thank you for the detailed reply, @verythorough!

Thank you @verythorough! This is exactly what I was looking for! And extra points for the security tip! Cheers!

1 Like

That’s weird. This was working for a minute and then… stopped. I started to receive Netlify-served 404 pages for the URLs mycustomdomain.com/wp-admin and mycustomdomain.com/wp-json instead of it actually working, like it did for a bit…

But looking at my published deploy summary, I noticed this:
image

This leads me to believe that the _redirects file that I set up, using @verythorough’s recommendation is not being processed. Here’s what I have:

/wp-admin https://wp.mycustomdomain.com/wp-admin 200
/wp-json  https://wp.mycustomdomain.com/wp-json  200

I’m not sure how it was working before. When I use curl to visit the URLs mentioned above, again I get a 404.

I can’t redeploy my site without it breaking, because for it to work it has to query /wp-json to compile correctly, so I can’t try moving the _redirects file to a different directory (right now it’s in the static directory using Gatsby. I read that this was the folder that gets compiled in the build and ends up in the build root).

In Netlify, I have my primary domain set up to use a DNS record that creates wp.mycustomdomain.com using an A record that points to the IP of my backend server. TTL is 60. (Also noteworthy is that the IP I’m pointing to is a shared server with multiple accounts. I’m not exactly sure how Netlify distinguishes which account to access without a “key path” as to which one to use).

At my domain registrar, I’m pointing the domain to Netlify’s nameservers. And have an A record set up pointing to your load balancer’s IP, plus a www CNAME record pointing to my default Netlify subdomain.

Interestingly too, around the same time, my SSL certification with Netlify and Let’s Encrypt appears to have broken. I get a warning dialogue under the HTTPS section in the Domain management section that says:

mycustomdomain.com doesn’t appear to be served by Netlify

I went through the troubleshooting with that and my page is being served by Netlify servers. Although it could have something to do with DNS cache, I have been constantly clearing the browser cache in my browsers and still the same results. It’s been a few days now. Update I have just tried flushing DNS cache for the A record for the root domain.

This strange behavior seemed to happen around the same time that I was setting up a domain name redirect for a second domain. At one point in my domain registrar’s (Google Domains) DNS settings for the secondary domain, I set up a www CNAME record that pointed to my primary domain URL. There was also a line in my Netlify _redirects file that referenced the domains together:

https://www.myseconddomain.com/* https://www.mycustomdomain.com/:splat 301! (all one line)

I’ve since removed any domain settings I had for the second domain in my Netlify backend UI. Currently, the only setting I have is with Google Domains, which redirects mysecondarydomain.com to mycustomdomain.com using their redirect system.

I’m at a loss… :confused:

Update:
I just saw that my static folder was not in the proper location in my Gatsby project. I’ve moved it but

Hi @songfarm-david, if your deploy is saying that there were no redirect rules processed then it is very likely that the _redirects file was not deployed with your site. If your static folder was in the wrong place, after moving it to the correct place, you will need to make sure that you latest deploy includes that change. Let me know if you still don’t see your redirect rules get processed.

Ok, I disable code that would break my build (by calling one of those broken URLs) and redeployed my site on Netlify to confirm that my redirects file was accepted.
image

I then tried to use curl and did curl -i https://www.mycustomdomain.com/wp-json and got a 500.

HTTP/1.1 500 Internal Server Error
Date: Tue, 30 Jul 2019 20:37:17 GMT
Content-Length: 0
Age: 0
Connection: keep-alive
Server: Netlify
X-NF-Request-ID: fxxxffe1-xxxx-xxxx-xxxx-xxxxxxxxxxxx-4816663

I’ve also been in contacts with the providers for Server A and they’ve instructed me that I’m supposed to create that subdomain on their server, too?

I’m not sure if I’m on the right track.

Thanks,

Hmm. Perhaps you should configure that subdomain as that provider states and see if that would work for you.

Regarding the 500 error, I can’t really say why you are getting a 500 without more specific information. If you would provide the X-NF-Request-ID (which is the id for that request and is used to make finding specific requests easier), I should be able to get a better idea what happened.

I was able to solve this. The key was to also create the subdomain on server A, where the WordPress site lives.

So in fact, you need to create the subdomain on both Netlify and the offsite server. In that way, the redirects mentioned above in the _redirects file work beautifully!

1 Like