0

I know how to do it with apache/mod_proxy, but I can't use mod_proxy on our shared host. So no .htaccess solutions.

Currently I have the following code in child theme's page.php

global $post ;
if( needs_replacement( $post ) ) {
  $post_name = $post->post_name ;
  $from_url = create_url( $post );
  $r = wp_remote_get( $from_url);
  echo wp_remote_retrieve_body( $r );
  exit(0);
}
require_once( get_template_directory() . '/page.php' );
?>

And it works. I am wondering what is the right way of doing this. $from_url is from the same server.

Example:

$post = https://example.com/docs/hello-world
$from_url = https://example.com/supp/?sec=hello-world

Making it clear, what I want to develop is a method that can get the rendered page content when I pass a URL. So something like:

function wp_get_url_content( $url ) {
  ....
}

The $url will always be a URL from the same server.

2
  • Do you mean the referrer? It's not 100% clear what you mean or why you're trying to do this, a little context as to the problem you're trying to solve would help us understand. Additionally, what do needs_replacement and create_url do and how do they work? – Tom J Nowell Feb 22 '19 at 15:21
  • Basically I have a plugin that provides a short code. When added to a page that creates a menu structure and the URLs of the format https://example.com/supp/?sec=hello-world from URL https://example.com/docs/hello-world. the URL https://example.com/docs/hello-world is a wordpress page. If I open it it shows me the page contents but without the additional menus provided by https://example.com/supp/?sec=hello-world. So whenever a user goes to https://example.com/docs/hello-world I want to show the content he gets when using https://example.com/supp/?sec=hello-world. – Dakshinamurthy Karra Feb 22 '19 at 16:16
0

Have WordPress (which creates the web server responses) respond with a redirect (via a call to wp_redirect()). Put as the first thing in header.php

global $post;
if( needs_replacement( $post ) ) {
  $from_url = create_url( $post );
  wp_redirect( $from_url );
  exit(0);
}
require_once( get_template_directory() . '/page.php' );
?>

(Note that you have to this first thing; otherwise, attempts to redirect will get ignored or more likely cause errors.)

6
  • An alternative (with greater distance from your approach) would be to use rewriting rather than redirecting. (Note: either can be done via WordPress rather than Apache.) In your case it might suffice to use add_rewrite_endpoint() – Loren Rosen Feb 23 '19 at 1:12
  • I know both solutions and actually using apache mod_rewrite currently for redirection. In these cases a 30x redirect is issued and the browser location shows the new URL that I want to avoid. I want to emulate something similar to redirect with mod_proxy. – Dakshinamurthy Karra Feb 23 '19 at 4:17
  • In that case, you probably want a rewrite, not a redirect. Rewrite should cause WordPress internally to run as if the modified URL had been given initially, but the browser need never know. In contrast, redirect should cause the browser (or, in your case, the proxy) to re-request with the new URL, which would be the same as Apache (with mod_alias or mod_rewrite) redirection.... – Loren Rosen Feb 23 '19 at 5:22
  • ...But I should double-check things -- there's a lot of confusion about (in the blogosphere ) because mod_rewrite can do both rewriting and redirecting. (Part of the confusion is that indeed you can emulate a rewrite via a proxy and a redirect.) A further point of possible confusion is that (though I should re-check my earlier work) it appears that WordPress mostly ignores attempts to rewrite via Apache, in favor of its own rewrite rules (for permalinks or via the calls alluded to earlier). – Loren Rosen Feb 23 '19 at 5:22
  • add_rewrite_rule did not help as it also changes the browser location. If I am wrong, can you post a simple example? – Dakshinamurthy Karra Feb 23 '19 at 10:31
0

(Thought of another approach)
Use a WordPress hook, and a call to set_query_var().
Something like

function add_my_query_vars () {
   set_query_var('sec', 'hello world'); 
}

add_action('pre_get_posts','add_my_query_vars');

(This does make some likely assumptions about how the plugin handles query variables.)

1
  • Doesn't help much. I used query_vars hook to add sec as a valid query var and added the sec to the URL. Doesn't help because this works only with `/supp' URL, but what we actually have is '/docs' URL. Updating the question with my understanding, please check it. – Dakshinamurthy Karra Feb 25 '19 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.