doutui7955 2016-12-20 12:59
浏览 65
已采纳

本地wordpress使用远程数据库,ubuntu,apache2

im trying to connect my local wordpress install to a db on a domain i own using the code provided here: https://coderwall.com/p/ck8v4a/remote-database-with-local-wordpress-instance

define('WP_CACHE', true); $currenthost = $_SERVER['HTTP_HOST'];
$mypos = strpos($currenthost, 'localhost/wpdir');
if ($mypos === false) { 
    define('WP_HOME','http://example.org/wpDir/');
    define('WP_SITEURL','http://example.org/wpDir/'); 
} else { 
    define('WP_HOME','http://localhost');
    define('WP_SITEURL','http://localhost/wpDir/');
}

with the above config i can open the site locally, it loads the navigation and recent post names correctly, so the db-connection seems to work. i cannot use links and pages are not found though.

if i add my /wpDir/ to the define('WP_HOME'... and/or strpos($currenthost,... it gets redirected to http://localhost/wpDir/home/ (as it should) but i get an URL not found error.

my localhost dir is standard /var/www/html this is where my local wordpress installations are.

any ideas how to fix this?

update: i am back to working on this and it would really help a lot if i could manage to use the remote database for my local testing i think it may be some kind of url rewriting problem but all my efforts to find a solution did not work...

  • 写回答

1条回答 默认 最新

  • douhan0562 2017-01-31 01:28
    关注

    not sure why it was so hard to find but i managed to do it (another way) basically by following the wp-codex here: https://codex.wordpress.org/Running_a_Development_Copy_of_WordPress using the drop-in method: the code in my db.php file looks like this:

    <?php
    // paste this in a (new) file, wp-content/db.php
    add_filter ( 'pre_option_home', 'test_localhosts' );
    add_filter ( 'pre_option_siteurl', 'test_localhosts' );
    function test_localhosts( ) {
      if (strcasecmp($_SERVER['REQUEST_URI'], '/localCopyOfSite') == 0
          || strcasecmp(substr($_SERVER['REQUEST_URI'], 0, 17), '/localCopyOfSite/') == 0) {
         return "http://localhost/localCopyOfSite/";
      }
      else return false; // act as normal; will pull main site info from db
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部