LoginSignup
1
3

More than 5 years have passed since last update.

.htaccess を使って旧アドレスを一括変換する方法 (concrete5 対応版)

Last updated at Posted at 2016-05-09

とあるサイトを concrete5に移植リニューアルすることになり、1000ページ近くあった旧サイトの URL の置換を1ページずつ行うと面倒くさかったので、正規表現を使い、Apache サーバーの mod_rewrite を使って .htaccess にルールを記入して、リダイレクト処理を簡単に行えるようにしました。

元記事
https://concrete5.co.jp/blog/htaccess-mod-rewrite-concrete5

concrete5 の標準には、追加 URL 機能というのがあって、追加 URL には、拡張子付きのページを設定できるのですが、1ページ毎に設定をする必要があるので、1000ページぐらいあるサイトだど大変なので、.htaccess の mod_rewrite の正規表現を使って対応することにしました。

※ このテクニックは Apache のみです。nginx では、conf ファイルを触る必要があります。

有償アドオン

また concrete5 には .htaccess のルールをより簡単に記述できる $30 の有償アドオン Redirectorを使うこともできます。 1ページずつリダイレクトルールを管理画面から入力したり、旧サイトの Google Sitemap XML を読み込んで各ページを指定する機能もついています。

※ このアドオンも .htaccess と mod_rewrite が使える Apache な Web サーバーが必要です。

前提条件

  • ドメインのルートに旧サイト、新サイトとも設置する。
  • mod_rewrite が使える。 (.htaccess に設定できる)
  • トップディレクトリに配置 (でなかったら RewriteBase を「/」から変更) する。

リダイレクトパターン

リダイレクトの必要なパターンを下記のように設定します。

パターン 旧サイト 新サイト 説明
#1 http://old.com/XXXX/index.php http://new.com/XXXX/ index.php は / に
#1 http://old.com/XXXX/index.htm http://new.com/XXXX/ index.htm は / に
#1 http://old.com/XXXX/index.html http://new.com/XXXX/ index.html は / に
#2 http://old.com/XXXX/index2.html http://new.com/XXXX/index2.html index[数字].html なファイルはそのままキープ。
#2 http://old.com/XXXX/about.html http://new.com/XXXX/about/ index.html 以外は .html 拡張子を取って / に
#2 http://old.com/XXXX/about.htm http://new.com/XXXX/about/ index.htm 以外は .htm 拡張子を取って / に
#3 http://old.com/XXXX/product.php http://new.com/XXXX/product/ index.php 以外は .php の拡張子を取ってスラッシュを加える

※ index[数字].html をそのままにしてリダイレクトしないという仕様は、concrete5 のエンタープライズアドオンである静的 HTML 出力で、ページリストブロックにページネーションがついた時に2ページ目以降が index2.html, index3.html というふうな URL で出力されるからです。

.htaccess サンプル

.htaccess というテキストファイルをサーバー上に作成 & 下記のコードをコピペしてください。

旧サイト、新サイトとも、ドメインのトップレベルに配置されている想定です。
ディレクトリ配下に設置されている場合は RewriteBase を変更してください。

RewriteEngine on
RewriteBase /

# concrete5 の設定
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]

# 1 index.html, index.html, index.php をリダイレクト
RewriteCond %{THE_REQUEST} ^.*/index\.(htm|html|php) [NC]
RewriteRule ^(.*)index.(htm|html|php)$ /$1 [R=301,L]

# 2 .html な拡張子の URL で index数字.html や index数字.htm 以外のファイルは拡張子を取ってリダイレクト
RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/+([^.]+)index\d{1,10}\.(html|htm)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.(html|htm) [NC]
RewriteRule ^ /%1 [R=301,L]

# 3 .php な拡張子の URL は、拡張子を取ってリダイレクト
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php [NC]
RewriteRule ^ /%1 [R=301,L]

また静的 HTML 出力を行っている場合は、動的に 404 ページの書き出しができないので、別途、.htaccess で設定する必要があります。

# エラーページ URL の設定
ErrorDocument 401 /error/401/index.html
ErrorDocument 403 /error/403/index.html
ErrorDocument 404 /error/404/index.html
ErrorDocument 500 /error/500/index.html

concrete5 へ旧サイトからページ移行するとき

ページ移行の際、
http://www.XXX.co.jp/news/news-20150503.php
http://www.XXX.co.jp/news/news-20150503.html
というページが旧サイトで存在していたら、concrete5 は
http://www.XXX.co.jp/news/news-20150503/
な URL でページを作成し、コンテンツを移行してください。

そうすると、上記の .htaccess を使うと、html や php が拡張子のページはすべて、自動的にスラッシュなページに遷移されます。

参考リンク

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