Wordpress NGINX将所有内容重定向到新域除

时间:2016-07-28 10:41:34

标签: wordpress nginx

我有一个不寻常的设置,我在http://example.com上运行了一个angularJS应用程序,它通过位于http://api.example.com的wordpress api提取数据。

http://api.example.com需要让/wp-login/wp-admin/wp-content/wp-includes网址正常工作,就像它仍然是常规的WordPress网站一样。

但是,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
}

1 个答案:

答案 0 :(得分:0)

将此代码放入WP Theme 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);

    }
 }
相关问题