WordPressのURL変更

Contents

 


はじめに

ブログ開設当初は実験的にサブディレクトリを切って複数のサイトを試す予定でしたが、結局今日までそれもやらずに来てしまいました。今となってはサイト管理やメンテナンスの面から考えてもサブディレクトリで運用する予定もないので、通常のドメイン直下の管理に変更(戻す?)しようと思います。

URL変更前

http://www.eastforest.jp/esthome/

URL変更後

http://www.eastforest.jp/

 

 

作業手順

ドキュメントルートバックアップ

とりあえずApacheで設定しているドキュメントルートごとバックアップしておきます。

# cd /var/www/html/
# tar zcvf eastforest.jp.tar.gz eastforest.jp

 

URL変更

WordPress の管理画面で 設定 > 一般 でURLを変更します。

 

サブディレクトリ移動

/var/www/html/eastforest.jp/esthome/ 配下を /var/www/html/eastforest.jp 直下に展開します。移動先のWordPress ディレクトリ直下にindex.phpと.htaccessがあることを確認します。

ls -la /var/www/html/eastforest.jp
total 204
drwxr-xr-x 5 apache apache 4096 Feb 21 13:46 .
drwxr-xr-x 3 root root 55 Feb 24 10:41 ..
-rw-r--r-- 1 apache apache 235 Feb 24 10:40 .htaccess
-rw-r--r-- 1 apache apache 418 Sep 25 2013 index.php
-rw-r--r-- 1 apache apache 19935 Feb 8 06:23 license.txt
-rw-r--r-- 1 apache apache 10303 Feb 8 06:23 readme.html
-rw-r--r-- 1 apache apache 5434 Sep 23 21:21 wp-activate.php
drwxr-xr-x 9 apache apache 4096 Feb 19 14:03 wp-admin
-rw-r--r-- 1 apache apache 364 Dec 19 2015 wp-blog-header.php
-rw-r--r-- 1 apache apache 1627 Aug 29 2016 wp-comments-post.php
-rw-r--r-- 1 apache apache 4954 Feb 19 14:18 wp-config.php
-rw-r--r-- 1 apache apache 3886 Feb 8 06:23 wp-config-sample.php
drwxr-xr-x 8 apache apache 112 Feb 24 10:44 wp-content
-rw-r--r-- 1 apache apache 3669 Aug 20 2017 wp-cron.php
drwxr-xr-x 18 apache apache 8192 Jan 17 09:31 wp-includes
-rw-r--r-- 1 apache apache 2422 Nov 21 2016 wp-links-opml.php
-rw-r--r-- 1 apache apache 3306 Aug 22 2017 wp-load.php
-rw-r--r-- 1 apache apache 36583 Oct 13 11:10 wp-login.php
-rw-r--r-- 1 apache apache 8048 Jan 11 2017 wp-mail.php
-rw-r--r-- 1 apache apache 16246 Oct 4 09:20 wp-settings.php
-rw-r--r-- 1 apache apache 30071 Oct 19 02:36 wp-signup.php
-rw-r--r-- 1 apache apache 4620 Oct 24 07:12 wp-trackback.php
-rw-r--r-- 1 apache apache 3065 Sep 1 2016 xmlrpc.php

 

index.phpの編集

今回の環境では変更は必要なかったですが、環境に応じて"/wp-blog-header.php" のパスを変更します。

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

 

リファラ許可の修正

httpd.conf で自ドメイン以外の画像直リンク禁止の設定をしていましたが、ここも修正しないと画像が表示されないので対応しておく。

# Referer Deny
<FilesMatch "\.(gif|jpg|jpeg|png)$">
 #SetEnvIf Referer "http://www.eastforest.jp/esthome/" authoritative_site
 SetEnvIf Referer "http://www.eastforest.jp/" authoritative_site
 #SetEnvIf Referer "https://www.eastforest.jp/esthome/" authoritative_site
 SetEnvIf Referer "https://www.eastforest.jp/" authoritative_site
 Require env authoritative_site
</FilesMatch>

 

Apache のserver-status と server-info の対応

今まではサブディレクトリにWordPressのコンテンツを置いていたので、Apache のserver-status と server-info の表示に影響はなかったが、今回の対応でNot Found(404)が表示されるようになってしまいました。
WordPressのリライトルールが原因でserver-status と server-info のときは場外する条件を入れる必要があります。ただしWordPressのアップデートで初期化されてしまうので注意。

# vi /var/www/html/eastforest.jp/.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !=/server-status
RewriteCond %{REQUEST_URI} !=/server-info
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

ちなみにWordPressのリライトルールのお陰で各記事の変更前URLは変更後URLに変換されるので、特にリダイレクトの設定はしてません。ただし変更前のサブディレクトリTOP(http://www.eastforetst.jp/esthome/)だとリダイレクトされたりされなかったり…いまいち挙動が不明。

 

記事の画像URLの変更

記事の中に組み込まれている画像のURLが変更前のままになっているので変更する必要があります。以前はSQLでreplaceしてましたが、今回はプラグインでURLを置換しました。使ったのSearch Regex で簡単でわかりやすかったです。

WordPressのURL変更などによる画像パスの一括置換
【常時SSL化】「Search Regex」画像URLを「http→https」に一括置換する方法【WordPressプラグイン】

 

参考

WordPress を専用ディレクトリに配置する