1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WordPressの.htaccessファイルをサブディレクトリにWPがインストールされている場合にスクリプトで対応させる方法

Posted at

はじめに

WordPressをサブディレクトリにインストールした場合、.htaccessファイルをサブディレクトリに対応したものに修正する必要があります。

.htaccess
# BEGIN WordPress
# "BEGIN WordPress" から "END WordPress" までのディレクティブ (行) は
# 動的に生成され、WordPress フィルターによってのみ修正が可能です。
# これらのマーカー間にあるディレクティブへのいかなる変更も上書きされてしまいます。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/subdir/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /path/to/subdir/index.php [L]
</IfModule>

この .htaccess ファイルをサブディレクトリ指定で動くように変換するためのスクリプト(sed)です。

bash
# !/usr/bin/env bash

# RewriteBase / ->  /path/to/subdir/
# DIR=/path/to/subdir/
DIR=${1:-/}
sed -i -e 's#^RewriteBase.*#RewriteBase '${DIR}'#' | \
  sed -i -e 's#^RewriteRule.*#RewriteRule . '${DIR}index.html' [L]#' 

このファイルを下記のように呼び出すと必要な部分が書き換わります。

$ ./rewrite_htaccess.sh /path/to/subdir > .htaccess
.htaccess
...
RewriteBase /path/to/subdir/
...
RewriteRule . /path/to/subdir/index.php [L]

困ったこと

WP-CLIや「設定>パーマリンク設定」から「更新」で、良い感じに.htaccessファイルを書き出してくれていたと思ったのですが、サブディレクトリにうまく対応できませんでした。できた気がするんですが…。

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?