1
votes

Recent Update - Updated for clarity

I have a WordPress site set up on Server A.

Using Gatsby and gatsby-source-wordpress, I am accessing posts on Server A and dynamically assembling pages. I am then deploying a Gatsby site to Netlify.

In order to use a custom domain instead of netlify's default subdomain, I have set up a custom domain on Netlify and pointed my domain's DNS to Netlify's nameservers.

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's servers instead of Server A, where my WordPress installation lives.

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,

3
Who is hosting the main domain's DNS you use to go to the Netlify site? How do you have the "alias" setup? What do you mean by redirecting "away" from the core WP files? Hard to help without some detailed specifics and the things you have tried, because there are a lot of ways this could be setup. 🙂talves
Is WP installed on the subdomain?ksav
@ksav No, I don't know immediately how I would install WP on Netlify or even if that's necessary.David Gaskin

3 Answers

1
votes

Below is not the right way but maybe it will help you.

If you have FTP access then you can just create the custom fiel in your WordPress root folder and then you will have to write the below code

<?php 
include "wp-load.php";
wp_set_auth_cookie($your_user_id); // generally 1 for the main admin user
?>

Run your newly created file like below http://example.com/custom_file.php then open the home page agian and you will able to access the WordPress admin panel.

0
votes

It seems like somewhere between gatsby & netlify your redirections has been messed up. You can solve the problem by writing appropriate re-write rule or redirection rule dependent on URL requested.

Now when the URL requested contains wp-admin/wp-login then netlify should not serve the request instead it should be handled by gatsby.

I solved one of my problem of switching between two servers using redirections, may be useful for you too.

0
votes

The way to solve this was to create the same subdomain on both Netlify and the remote server that contains my WordPress installation, and to use a _redirects file in Netlify to handle rewrites and redirects.

For example, I'll create wp.mydomain.com as my subdomain.

Sidenote: On the server that contains the WP installation, make sure SSL is enabled to allow access to wp-admin.

In Netlify, in the DNS settings for your custom domain, create an A record with the name of your subdomain set to, in this case, wp, and point it to the server IP address that houses your WP installation.

Next, in the root of your project, create a _redirects file (in my case, using Gatsby, I create the static folder inside the root of my project - not src) and place the following rules:

/wp-admin  https://wp.mydomain.com 200

This says that anytime I access www.mydomain.com/wp-admin redirect me to wp.mydomain.com/wp-admin, the subdomain I set up on my WP server. This routes my request to my WP server and not Netlify.

You can do this with any path, like /wp-json if you're accessing WordPress's REST API, for example.

This was a pain to get working. I hope it helps someone out there!