Wordpress redirect to main host - wordpress

I am looking for a WP php rule to always serve any webpage via the desired host without adding a redirect rule which is not an option in this case. see below.
We have our main site on one server and a WordPress installed on another server. We use cloudflare workers to serve the blogs content (blog.domain) via a subdirectory of the main server (domain.com/blog).
To avoid some issues on the admin side of WP we had to modify the wp-config.php to specify the correct host which would be the main domain/blog
$_SERVER['REQUEST_URI'] = '/blog' . $_SERVER['REQUEST_URI'];
$_SERVER['SCRIPT_NAME'] = '/blog' . $_SERVER['SCRIPT_NAME'];
$_SERVER['PHP_SELF'] = '/blog' . $_SERVER['PHP_SELF'];
$_SERVER['HTTP_HOST'] = 'www.domain.com';
source: https://core.trac.wordpress.org/ticket/36201#comment:17
What I noticed afterward is that (1) WP admin will always redirect the subdomain to the desired host, so if I go to the wp admin panel via the original subdomain it will redirect to the main-domain/blog/wp-admin.
( 2) Even when I hit a front end URL and I am logged in to the WP admin the same will happen, the host will be replaced.
(3) if I am not logged in to the admin panel but I am using a URL with a query parameter such as
https://sub.domain.com/?page_id=3
it will redirect to the desired host with the clean URL.
My question is how can I get WP to always redirect to the desired host? Using a simple redirect rule in PHP or apache/Nginx will result in an infinite loop (since the sub-directory is relying on the subdomain for the content)

Related

Netlify Custom Domains and WordPress admin access

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,
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.
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.
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!

Wordpress with ngrok redirect me for localhost

i have a site in wordpress and i want to share to my client but with ngrok when i put the link of ngrok he redirect for localhost.
I have tried relative urls plugins but still doesnt work
You can use a stand alone script like the one of interconnectit.com to search and replace all urls in your database with the ngrok one.
You'll need to search :
http://localhost:8888/site (replace with your own local url)
and replace with
https://your-ngrok-url.com
It's WP that automatically redirects the user to the "correct" domain name.
A quick fix for this is to add/edit these two lines to the wp-config.php file:
define('WP_HOME', 'https://your-ngrok-url.ngrok.io');
define('WP_SITEURL', 'https://your-ngrok-url.ngrok.io');
Remember to remove them again, when you are done testing.

Wordpress https:// broken home page and admin area

My wordpress website is using cloudfare and I desided to make my website from http:// -> https://
I installed Wordpress HTTPS and SSL Insecure Content Fixer plugins and changed URLs in Settings -> General (from http:// to https://)
This made my home page & admin area show "ERR_TOO_MANY_REDIRECTS". ALL other pages work great with the green https:// bar
I tried make things which I found on google: adding lines to wp-config.php or .htaccess But either it doesn't change anythin or it breaks other pages too.
Website is hosted on Ubuntu server.
P.S. Only if I replace https:// to http:// (through the mysql) I can access admin area and home page BUT they load without css styles.
Add this code to your wp-config.php (before $table_prefix = 'wp_'; line) and it will work.
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
$_SERVER['HTTPS']='on';
define('FORCE_SSL_ADMIN', true);
The first block "tells" PHP that your website is already using https. (you need to tell it because it is not local apache https, but 3rd party out-server SSL). Second one forces wp-admin always use SSL.

Sub-domain as sub-directory in URL

We need solution for our new server, we have one main domain e.g. example.com and its hosted on VPS this website is in ASP.Net with blog e.g. example.com/blog , The blog is in sub-directory and using Wordpress. We got new server and we want to move our blog on new VPS so we created sub-domain on main domain e.g. blog.example.com and change the A Record in control panel and blog.example.com is working fine, but we want to show URL like this example.com/blog/ is there a any way we can show sub-domain as a sub-directory ? The main website will remain host on main server we only need to work with sub-domain. So it shows all links like, example.com/blog/post-name/
Try this in your .htaccess:
RewriteEngine On
RewriteRule ^blog/(.*) http://blog.example.com/$1 [P,L]
It uses mod_proxy (and will fail if it's not available) to reverse proxy the traffic beneath /blog/. Obviously, if you have a lot of traffic or bad connection between the two servers, you will want something that does cache, but usually works quite good.
You will probably have to set
define('WP_HOME', 'http://example.com/blog');
define('WP_SITEURL', 'http://example.com/blog');
in your wp-config.php so the internal links point to the URL you want them to work as.

Run multiple domains under 1 wordpress instance

I have a question. I am trying to set up two domains under one wordpress instance.
For instance, I have a wordpress site on www.site1.com
I have another domain called www.site2.com. Now, I want to run the same wordpress site that is under www.site1.com on www.site2.com
I tried using the wordpress domain mu mapping plugin
http://wordpress.org/plugins/wordpress-mu-domain-mapping/installation/
But it only allows me subdomains or directories of www.site1.com
Can anyone help?
Add
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] );
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] );
to wp-config.php.
If you meant pointing 2 or more domain names to a single wordpress install, answer is yes you can but it won't work. Wordpress is designed only to host one domain name, if you point another domain name to your wordpress, it will return 404. The other way around to do this is redirect your other domain names to your main domain name (i.e. redirect website2.com to website1.com)
If you meant pointing 2 or more domain names to a Wordpress Multi site install, here's an article that will help you setting up domain mapping in your Wordpress multisite: https://rtcamp.com/wordpress-nginx/tutorials/multisite/domain-mappinng/
We need to set WordPress Multisite Network to make it work. I have successfully set my wordpress v4.1 installation to work for multiple sites and multiple domains by following this and this.
Please note that you will have to make sure editing to php files correctly otherwise that will completely crash your site if you make a mistake. Moreover a correct setting of htaccess is also important.

Resources