Dan Safee
Dan Safee

Reputation: 1618

Wordpress NGINX redirect everything to new domain EXCEPT

I have an unusual setup in that I have an angularJS application running on http://example.com which pulls data via the wordpress api located at http://api.example.com.

http://api.example.com needs to have the /wp-login, /wp-admin, /wp-content, and /wp-includes urls to work as if it is still a regular wordpress site.

However all other url's like http://api.example.com/category/excategory or http://api.example.com/this-is-a-post-title need to redirect 301 to the http://example.com domain.

example:

http://api.example.com/category/excategory

redirects to

http://example.com/category/excategory

but

http://api.example.com/wp-admin (and anything after it)

does not.

I've tried all kinds of crazy things, but my location blocks seem to either conflict, or I get weird url's that go to nowhere.

Here's a try that failed:

location ~ /wp-(?:admin|login|includes|content) {
index index.php;
try_files $uri $uri/ /index.php?$args;
}

location / {
   return 301 $scheme//example.com$request_uri
}

Upvotes: 1

Views: 922

Answers (1)

Shrikant D
Shrikant D

Reputation: 837

Put this code in your WP Theme Functions.php file. It should redirect all urls except the one who contain wp-admin:

 add_action('init','_redirect_api_url');

 function _redirect_api_url(){
    $redirect = TRUE;
    $pathNotToRedirect = array('wp-admin','wp-content', 'wp-login','wp-includes');
    $path = "http://example.com".$_SERVER['REQUEST_URI'];
    foreach($pathNotToRedirect as $val){
        if(strpos($path, $val)){
           $redirect = FALSE;
           break;  
        }
    }
    if($redirect == TRUE) {
       header("Location: ".$path);

    }
 }

Upvotes: 1

Related Questions