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

Hi, I encounter this problem, too, and I have tried the solutions mentioned in this forum. However, other problem persist when I tried to access wp-admin in my subdomain, yet I got redirected to mycustomdomain.com/admin/wp-login.php (which is not in the A record in netlify) (it supposed to redirect to admin.mycustomdomain.com/wp-login.php). Anyone knows a workaround for this problem? Thank you

Did you create a redirect rule to handle that redirect? If so, what is the rule that was created?

Also, would you be willing to share the URL for this Netlify site?

Would you please let us know your preference?

ok here is my take. I have been struggling for hours getting the above mentioned solution to work for me. It just didnt wanna work! my redirection /wp-admin always sent me back to my netlify site instead of the wordpress subdomain of server A.

Here was the problem/solution. Dont create a subdomain aka domain alias on netlify. I create the subdomain cms.mydomain.com on server A and domain alias cms.mydomain.com on netlify. then changed the DNS setting for cms.mydomain.com on netlify to point to my subdomain on server A but I never got there. The netlify subdomain always pointed back to netlify.

My solution, dont create a domain alias on netlify. create ONE Subdomain on server A for the wordpress installation and only add a new DNS record on netlify, in my case cms replacing the @. With that in place the redirects mentioned above work.

Thanks for sharing your take on this, @oppenberger! I’m glad you were able to get it to work.

If anyone has any thoughts on this, please share - we’re happy to keep this thread open.

@oppenberger
I think I had the same problem as you, my sub-domain, where my back is stored, sends me back this error :
Hello request to https://subdomain.domain.fr/ failed, reason: getaddrinfo ENOTFOUND https://subdomain.domain.fr/ https://subdomain.domain.fr/:443’,
I create my sub-domain on server A. Now what am I supposed to do on netlify?

1 Like

Hey Kamsou, sorry for being a bit unclear about my solution. Also, I changed my method described above as it didn’t fully work as intended.
Try the following…
Go to your C-Panel of your host (in my case Bluehost)

  • Change the A-Record of your root domain to point to Netlify’s IP address 104.198.14.52
    (in my case the root domain is just the @ symbol, @ => 104.198.14.52 14400)

  • Change the CNAME of www to yourDomain.netlify.app
    (in my case: www => myDomain.netlify.com 14400)

Doing this will redirect any traffic for www.yourDomain.fr and yourDomain.fr to your Netlify site and any other subdomains of your root domain won’t be redirected. This is important if you have subdomains you don’t want to sent to Netlify.

On Netlify I have 3 custom domains (no additional DNS records)

myDomain.netlify.app

Default subdomain

myDomain.com

Primary domain

www.myDomain.com

Redirects automatically to the primary domain

I hope I am clear enough about how I got my sites working and it will work for you as well.
Give it a try and let us know if it works.

Thansk for response @oppenberger !
Yes I have this config for my root domain and its work, it’s just my subdomain api.myDomain.fr who doesn’t work and say me “Unable to find the IP address of the api.myDomain.fr server.”


Currently I have this in my DNS for my sub-domain. My host generated the IP automatically so I don’t understand.
Say me tell me if you’ve had to do a config for your subdomains?

But, my host has a alert : " IONOS DNS settings are inactive if you are using a custom name server." So maybe that’s what’s holding up.
I’m thinking about buying another domain name to store my back…

I didn’t have to do any additional configuration on my subdomains. I am not sure but this sounds like an issue with your host. Have you tried contacting them and explaining your problem?