简体   繁体   English

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

[英]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://example.com上运行了一个angularJS应用程序,它通过位于http://api.example.com的wordpress api提取数据。

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. http://api.example.com需要有/wp-login/wp-admin/wp-content/wp-includes url才能正常工作,就像它仍然是一个普通的wordpress网站一样。

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. 但是所有其他网址如http://api.example.com/category/excategoryhttp://api.example.com/this-is-a-post-title需要将301重定向到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
}

Put this code in your WP Theme Functions.php file. 将此代码放在WP Theme Functions.php文件中。 It should redirect all urls except the one who contain wp-admin: 除了包含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);

    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM