LoginSignup
1
4

More than 5 years have passed since last update.

二つのWordPressを合体した時のことをサルにもわかるようにメモしとく!

Last updated at Posted at 2018-04-24

はじめに

サーバーのWordPressの移行を頼まれたのですが、WordPress初めて、サーバーの知識不足で苦労しました。
通常はあまりないことなので、忘れないように記録として残しておきます。

1.要件

下記が別々のサーバーで動いていいるサイトの結合

中途採用のページだけがまだ利用中。
http://www.hogehoge.jp/recruit/

採用ページの下に中途採用のフォルダとして参照可能する。
https://www.newhogehoge.com/recruit/career/

実際のフォルダは前記の通りでなくて仮想的なものでよい。

2.環境条件について

4つのサーバーがあります。

旧環境

  • サーバー:レンタルサーバー(SSL無し)
  • OS:FreeBSD 8.4-RELEASE-p11
  • FTP:使用可能 /html公開するルートディレクトリ
  • SSH:利用不可
  • PHP:Version 5.2.8 (system()関数利用可能)
  • CMS:WordPress Version
  • DB:MySql 5.0
  • DB接続ツール:不明なので自分でphpMyAdmin phpMyAdmin 5.5インストール
  • DB接続名は、WordPressの wp-config.php より取得
  • WordPress バージョン:3.4 マルチサイト マルチサイトかどうかはDBのテーブルにxxxxとxxxxがあるかどうかで判別可能 もちろん、他の方法もある。

フォルダ構成

    html
      →wp_admin(ディレクトリ)
      →wp-content(ディレクトリ)
      →wp_include(ディレクトリ)
      →wp-login.php
      →wp-index.php 以下は内容
          define('WP_USE_THEMES', true);
          require('./wp-blog-header.php');
      ** →.htaccess 以下は内容 **
      AddHandler x-httpd-php528 .php
      Options +FollowSymLinks
      suPHP_ConfigPath /home/aa142qrutc/

      DirectoryIndex index.php index.html index.cgi
      AddDefaultCharset UTF-8
      AddType "text/html; charset=utf-8" html
      AddType "text/html; charset=utf-8" htm

      RewriteEngine On
      RewriteBase /
      RewriteCond %{QUERY_STRING} ^s=&a=&v=&m=&g=&c=280&b=&b_name1=%E6%A4%9C%E7%B4%A2%E3%81%99%E3%82%8B(.*)$
      RewriteRule ^shops/$ https://www.hogehoge.jp/search?shop=&prefecture=&stockholder_benefit=on [R=301,L]

      RewriteEngine On
      RewriteBase /
      RewriteCond %{QUERY_STRING} ^s=&a=&v=&m=&g=&c=280&b=&b_name1=検索する(.*)$
      RewriteRule ^shops/$ https://www.hogehoge.jp/search?shop=&prefecture=&stockholder_benefit=on [R=301,L]

      # ニュースのアーカイブリダイレクト
      RewriteEngine On
      RewriteBase /
      RewriteCond %{QUERY_STRING} ^y=(.*)$
      RewriteRule ^news/$ https://www.hogehoge.jp/features/archives/%1/? [R=301,L]

      RewriteEngine On
      RewriteBase /

      ## For https://www.hogehoge.jp/
      RewriteRule ^nabefair/$ https://www.hogehoge.jp/event/nabefair/ [R=301,L]
      RewriteRule ^event/$ https://www.hogehoge.jp/features/categories/pickup/ [R=301,L]
      以下ずらずらと別のサイトにリダイレクト

      # uploaded files
      RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

      # add a trailing slash to /wp-admin
      RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

      RewriteCond %{REQUEST_FILENAME} -f [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^ - [L]
      RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
      RewriteRule  ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
      RewriteRule . index.php [L]

新環境

  • サーバー:レンタルサーバー(SSL有り)
  • OS:10.1-RELEASE-p41
  • FTP:使用可能 /html公開するルートディレクトリ
  • SSH:利用不可
  • PHP:Version 5.5.23 (system()関数利用可能)
  • CMS:WordPress Version
  • DB:MySql 5.5.42
  • DB接続ツール:コントロールパネル+phpMyadmin利用可能(V4.4 100MBインポート可能に拡張)
  • DB接続名は、WordPressの wp-config.php より取得
  • WordPress バージョン:4.9.3 シングルサイト マルチサイトかどうかはDBのテーブルにxxxxとxxxxがあるかどうかで判別可能 今回の件は、旧サーバーにテスト用のサイトを構築していたためマルチサイトになっていたと思われます。 DBは別々になっていましたがなんでだろう?疑問は残りました。
  • その他 xxx.com/recruit/ というフォルダはなく、WP側で管理している。
  • フォルダ構成
  html
   →wp
      index.html
       内容
        define('WP_USE_THEMES', true);
        require( dirname( __FILE__ ) . '/wp/wp-blog-header.php' );
      →wp_admin(ディレクトリ)
      →wp-content(ディレクトリ)
      →wp_include(ディレクトリ)
      →wp-login.php
      →wp-index.php 以下は内容
        define('WP_USE_THEMES', true);
        require('./wp-blog-header.php');
      ** →.htaccess 以下は内容 **
        Options +SymLinksIfOwnerMatch
        
        # Enforce SSL access
        
        <IfModule mod_rewrite.c>
            RewriteEngine on
            RewriteCond %{HTTPS} off
            RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
        </IfModule>
        
        #-------------------------------------------------
        # Access Restriction
        
        <Files "xmlrpc.php">
            order deny,allow
            deny from all
        </Files>
        
        <Files "wp-login.php">
            AuthName "Input ID & Password"
            AuthType Basic
            AuthUserFile /usr/home/user001/.htpasswd
            Require valid-user
        </Files>
        
        #-------------------------------------------------
        
        # BEGIN WordPress
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        </IfModule>
        
        # END WordPress

3.基本的に行う作業(順不同)

  • 新サイトWebルート直下に以下を作成
recruitフォルダとcareerフォルダを作成
  /html/recruit/
  /html/recruit/career
index.phpファイルを作成する。フォルダが参照されたら上位リクルートサイトに移動するように編集。
  内容
   define('WP_USE_THEMES', true);
   require( dirname( __FILE__ ) . '/../wp/wp-blog-header.php' );
  • 既存サイトのfunctions.phpを修正(リダイレクトループの回避)

/www/html/wp/wp-content/themes/production/functions.php
最後の行に(リダイレクトループしちゃうので・・・)
remove_filter('template_redirect', 'redirect_canonical');


例.修正前

<?php
/*
 * functionsフォルダにあるファイルをすべて読み込む
*/
foreach (glob(TEMPLATEPATH."/functions/*.php") as $file) {
    require_once $file;
}

例.修正後

<?php
/*
 * functionsフォルダにあるファイルをすべて読み込む
*/
foreach (glob(TEMPLATEPATH."/functions/*.php") as $file) {
    require_once $file;
}
remove_filter('template_redirect', 'redirect_canonical');

  • 引っ越しするサイト(=旧環境)DBのテーブル内容3つを修正

  • wp_site
      domain:ddnew.gluesd.work
      path:/recruit/career/

  • wp_blogse
      domain:ddnew.gluesd.work
      path:/recruit/career/

  • wp_options


    optionname項目がsiteurlのレコードのURL
    https://www.hogehoge.com
       →https://www.newhogehoge.com/recruit/career
    同、homeレコードも同様に設定
    https://www.hogehoge.com
       →https://www.newhogehoge.com/recruit/career
    同、fileupload_url
    https://www.newhogehoge.com/wp-content/uploads
       →https://www.newhogehoge.com/recruit/career/wp-content/uploads

・移行するしたサイトのDB接続情報を修正

ファイル:wp-config.php

define('DOMAIN_CURRENT_SITE', 'www.newhogehoge.com');
define('PATH_CURRENT_SITE', '/recruit/career/');

DBの設定は新サイトにあるものをコピーして、DB名だけ変えるのが吉。

  • テンプレートの修正
   /www/ddnew/recruit/career/wp-content/themes/hogehote
   archive-recruit.php
     →/html/recruit/career/index.php
   にコピーします。
   これは、WPが制御してarchive-recruit.phpを開いていたものを、フォルダ名で開ける?????
  • 今回独特の変更

一覧から選択した時に、詳細が表示されなかったのでCSSを修正
/www/ddnew/recruit/career/wp-content/themes/common/css/recruit/recruit.css

601行目をコメントアウト

#recruit_detail{
/*      visibility: hidden; */
        position: relative;
  • .htaccessの修正

careerの下の.htaccess

RewriteRule ^/recruit/career/r/$ /recruit/career/ [R=302,L]

を追加します

以上です。ご意見、アドバイス、間違いを発見したら教えて頂けると嬉しいです。

最初は色々と試行錯誤しましたが、解決できずに経験豊富な上司に助けてもらいました。
というか、全部やって貰った感じです。
おまけに、簡易手順書迄作って貰ってしまって。。。感謝です。

おまけ

おまけとして苦しんだりつまずいたりしたところを残しておきます。

  • 旧サイトでDBアクセスする方法がなくて、phpMyadminの古いものを探してきて入れました。環境との相性があるんですね。
  • 新サーバーがにphpMyAdminが100MBまでのインポートを対応していてくれたので助かりました。
  • 一番はまったのは、SSLです。WPとの相性があるらしく、VMで作成したFreeBSD 10.3では動作してくれませんでした。古いWPなら大丈夫との話もありましたが、そういうわけにはいきませんね。
  • ディレクトリにアクセス権限付けるのを忘れてインターナルサーバーエラーが出た。ログを見たらSSLのエラーログに載っていた。なんでSSLなんだろう。
  • sqldumpが使えなかったので、PHPでそれを実行できるようにしました。バージョンが古いのかもしれませんが、phpMyAdminではサイズに関係なくダンプが切れてしまいとても悩みました。 サンプルソースです。サーバーの状況を調べたり、圧縮したり色々役に立ちました。 ただ、普通のレンタルサーバーだと制限があるかと思います。
    <?php
        //echo exec('whoami');
        //phpinfo();
        echo "\n <pre> --- command --- \n";
        //var_dump( system('ls', $ret) );
        //var_dump( $ret );
        //set_time_limit(0);
        //$cmd = 'apache -v';

        $cmd = 'pwd';
        var_dump( system($cmd, $ret) );
        var_dump( $ret );

        //$cmd = 'ls *.sql';
        //var_dump( system($cmd, $ret) );
        //var_dump( $ret );
        /*
        $cmd = 'zip -r /var/www/vhosts/crosshotel.com/server_migration201803/httpdocs/aaaa.zip /var/www/vhosts/crosshotel.com/httpdocs/aaaa';
        var_dump( system($cmd, $ret) );
        var_dump( $ret );
        */
        echo "\n  --- command --- \n</pre>";
    ?>
  • 新サーバーにsellinuxがどうも入っているらしく、.htaccessが読めないと言われる。どうもファイルやディレクトリに権限を与えすぎるとダメらしい。
  • .htaccess に Options +FollowSymLinksが入っていた。コメントアウトした。
  • ソースがそのまま表示されちゃう。 いろいろやったんで、何が理由だったのかわからなくなっちゃいました。
  • Fatal error?困った Fatal error: Call to undefined function get_stylesheet_directory() in /usr/home/chz3019002/html/recruit/career/index.php on line 2 これは単に、コピー先間違えただけなんだけど、結構気が付けなかったです。

ちょっとだけ便利に

ちょっとだけ便利なPHPを作成したのでここに残しておきます。
前節で作ったPHPでのコマンド実行を多少使いやすくしただけですけど、最近Teratermの使えないレンタルサーバーへの移行も頼まれたりするので、汎用的に作ってみました。
危険なので使い終わったら削除は必須。

** index.html **


<!DOCTYPE HTML>
<html lang="ja">
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="cache-control" content="no-cache" />
        <meta http-equiv="expires" content="0" />
        <title>command exec</title>
    </head>
    <frameset rows="50,*">
        <frame src="exec.html" name="command" title="command">
        <frame src="" name="response" title="respons">
    </frameset>
</html>

exec.php

<!DOCTYPE HTML>
<html lang="ja">
    <head>
        <meta charset="<?php echo $_POST["charset"]; ?>">
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="cache-control" content="no-cache" />
        <meta http-equiv="expires" content="0" />
    </head>
<body>
<?php
//set_time_limit(5);

echo "\n <pre> ---------- \n";

$cmd = $_POST["command"];
echo 'input command => '.$cmd."\n\n";

system($cmd, $ret);
var_dump( $ret );

echo "\n  ---------- \n</pre>";

?>

</body>
</html>

exec.html


<!DOCTYPE HTML>
<html lang="ja">
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="cache-control" content="no-cache" />
        <meta http-equiv="expires" content="0" />
        <title>
            line command execcute
        </title>
    </head>
    <body>
        <form method="post" action="exec.php" target="response">
            <p>command <input type="text" width= 150 name="command" size=60 /> <input type="submit" value=" exec "> 
             <select name="charset">
                <option value="utf-8">utf-8</option>
                <option value="Shift_JIS">Shift_JIS</option>
                <option value="EUC-JP">EUC-JP</option>
                <option value="iso-8859-15">iso-8859-15</option>
            </select>

        </form>
    </body>
</html>

説明するのがおっくうなので、各ファイルにコピペして動かしてみてください。
1行で終わるコマンドならいけるかなと思います。

ご要望あればそのうち説明を書きますね。
それでは。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4