2016-07-28 53 views
1

我有一個不尋常的設置,我有一個angularJS應用程序運行在http://example.com,它通過位於http://api.example.com的wordpress api提取數據。WordPress的NGINX重定向到一個新的領域除了

http://api.example.com需要有/wp-login/wp-admin/wp-content,和/wp-includes網址,就好像它仍然是一個普通的WordPress網站的工作。

但是所有其他的URL像http://api.example.com/category/excategoryhttp://api.example.com/this-is-a-post-title需要301重定向到http://example.com domain.

例如:

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

重定向到

http://example.com/category/excategory 

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

沒有。

我嘗試了各種瘋狂的東西,但我的位置塊似乎要麼衝突,要麼我得到奇怪的網址,無處可去。

這裏的失敗嘗試:

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

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

嘗試更新您的永久鏈接結構。看看你的.htaccess文件是否可寫。 –

+0

考慮到這是一個NGINX設置,.htaccess會是一個合適的解決方案嗎? –

+0

嗯,.htaccess不是一個合適的解決方案。嘗試將永久鏈接結構設置爲默認,並且看到您可以訪問網頁。 –

回答

0

將這個代碼在你的WP主題functions.php文件。它應該重定向除了包含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); 

    } 
} 
+0

這也會重定向/ wp-content/wp-login和/ wp-includes?我需要那些留在api.example.com –

+0

@DanSafee:上面的代碼應該工作。 –

相關問題