Support » Developing with WordPress » Pass external url trough if page not found

  • Hello,

    Is it possible to pass a external content form other domain if a 404 happens?
    We installed a wordpress site (www.woefkesranch.fr) but the old content should be visible too (fr.woefkesranch.be). So when you visit https://www.woefkesranch.fr/navigation/images/facebook-fr.jpg and not found, it should look at https://fr.woefkesranch.be/navigation/images/facebook-fr.jpg but without changing the domain
    I already found this, http headers aren’t pass.

    
    function wpd_do_stuff_on_404(){
        if( is_404() ){
            // do stuff
    		global $wp;
    		$current_url = home_url(add_query_arg(array(),$wp->request));
    		//set $url to what's needed 
    $url = str_replace('www.woefkesranch.fr','fr.woefkesranch.be',$current_url);
    		$response = wp_remote_get($url);
    		$response_code = wp_remote_retrieve_response_code( $response );
    		//echo $response_code;
    		if ($response_code == 200){
    			//status_header(200);
    			echo wp_remote_retrieve_body( $response );
    			die();
    		}		
        }
    }
    add_action( 'template_redirect', 'wpd_do_stuff_on_404' );

    Any suggestions?

Viewing 3 replies - 1 through 3 (of 3 total)
  • sorry found a solution, but not with wordpress.
    if i put the next code in htaccess file above the wordpress code it works:

    
    RewriteCond %{HTTP_HOST} woefkesranch.fr$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://fr.woefkesranch.be/$1 [NC,P]

    sorry i’m to quickly with asking, and i got this idea after 3 hours of searching wordpress

    edit:
    this breaks wordpress of course! grrr.
    any solutions?

    • This reply was modified 2 months, 2 weeks ago by  tomwoef.
    Moderator bcworkz

    (@bcworkz)

    Why don’t you mirror the old site’s content that should still be accessible on the new site? Redirect any old site requests to the new site. Of course the domain would change, but in this case it’s old to new, not new getting from old, so maybe this change is acceptable. Redirect with status 301 and eventually the old domain will stop being requested.

    You would be better of putting in redirects so traffic to your site gets to the correct content. By putting in content from another site on the old site might cause you issues where SEO is concerned and you may ranked lower in search. I’d suggest using a redirect manager to take visitors to the right page.

    Plugins are available to do this, but you can also do it manually through .htaccess rules but that’s advisory. It takes more time and effort to manage.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.