ということで、SEOに適したUrlを吐き出してくれるプラグイン「SEO Strict URLs」(詳細は、こちら を参照)があるんですが、現在作業中のサイトが旧URLから新URL(ホスト変わるので)に変わるので、軽めな対応で済ませたいという思いでプラグインを利用せずにコアをちょっぴり修正してフォルダの場合のURLをそれっぽいURLで吐き出すようにしてみました。
例えば、下記のようなドキュメント構造になっていた場合
/FolderA
|--- DocumentA1
|--- DocumentA2
/DocumentB
サフィックスが.htmlと付いていた場合に、
FolderAへのアクセス時には、FolderA.htmlに
DocumentA1へのアクセス時には、FolderA/DocumentA1.html とMODxがAliasを作ってくれます。
サフィックスがない場合は、前者がFolderA、後者がFolderA/DocumentA1となります。
で、FolderAへのアクセス時は、FolderA/と後ろに/を付けた形式でURLを吐き出しもらうようにしたいわけです。
サフィックスがある場合だとFolderAとFolderA.htmlとこの時点でちょっとなぁってことなので、SEO Urlsではサフィックスの有無に関係なくFolderAにはFolderA/でアクセスでき
ということで、SEO Strict Urlsプラグインの基本的なURL生成部分をコア修正で補うパッチになります。
但し、条件としてサフィックスなしでユーザがリーフ(末端のドキュメントである)を認識して自分でエイリアス名に拡張子を含めたものを設定する必要があります、もちろん、フォルダとしての扱いの場合は拡張子は付けちゃなりません。
というのは、コアの修正に際してはこの拡張子(というかピリオド)があるかないかで、/を付けるか付けないかを決めているわけです。
サフィックス付きだと、もうちょっと面倒なことをしなければならないので・・・そこまでするならSEO strict Urlsプラグインを使った方がよいのです。
もちろん、URL生成部分に修正を加えるだけなので、FolderAでアクセスされた場合にFolderA/にリダイレクトしてくれることはしません。
なので、すでに構築済みの場合はSEO Strict Urlsプラグインを導入することをオススメします。
もちろん、過去のことは忘れて心機一転という方はコア修正で済ませてもOKです。
対象ファイルは、document.parser.class.inc.phpです。(ちなみに、バージョンは0.9.6.2p2)
修正するべき関数を丸ごと以下に記載するので、入れ替えればOKです。
1.makeFriendlyURL関数
function makeFriendlyURL($pre, $suff, $alias) {
$dir= dirname($alias);
$balias = basename($alias);
if (!strpos($balias,"."))
{ $balias .= "/";
}
return ($dir != '.' ? "$dir/" : "") . $pre .$balias . $suff;
}
2.makeUrl関数
function makeUrl($id, $alias= '', $args= '', $scheme= '') {
$url= '';
$virtualDir= '';
if (!is_numeric($id)) {
$this->messageQuit('`' . $id . '` is not numeric and may not be passed to makeUrl()');
}
if ($args != '' && $this->config['friendly_urls'] == 1) {
// add ? to $args if missing
$c= substr($args, 0, 1);
if (strpos($this->config['friendly_url_prefix'], '?') === false) {
if ($c == '&')
$args= '?' . substr($args, 1);
elseif ($c != '?') $args= '?' . $args;
} else {
if ($c == '?')
$args= '&' . substr($args, 1);
elseif ($c != '&') $args= '&' . $args;
}
}
elseif ($args != '') {
// add & to $args if missing
$c= substr($args, 0, 1);
if ($c == '?')
$args= '&' . substr($args, 1);
elseif ($c != '&') $args= '&' . $args;
}
if ($this->config['friendly_urls'] == 1 && $alias != '') {
$url= $this->config['friendly_url_prefix'] . $alias . $this->config['friendly_url_suffix'] . $args;
}
elseif ($this->config['friendly_urls'] == 1 && $alias == '') {
$alias= $id;
if ($this->config['friendly_alias_urls'] == 1) {
$al= $this->aliasListing[$id];
$alPath= !empty ($al['path']) ? $al['path'] . '/' : '';
if ($al && $al['alias'])
$alias= $al['alias'];
}
if (!strpos($alias,"."))
{ $alias .= "/";
}
$alias= $alPath . $this->config['friendly_url_prefix'] . $alias . $this->config['friendly_url_suffix'];
$url= $alias . $args;
} else {
$url= 'index.php?id=' . $id . $args;
}
$host= $this->config['base_url'];
// check if scheme argument has been set
if ($scheme != '') {
// for backward compatibility - check if the desired scheme is different than the current scheme
if (is_numeric($scheme) && $scheme != $_SERVER['HTTPS']) {
$scheme= ($_SERVER['HTTPS'] ? 'http' : 'https');
}
// to-do: check to make sure that $site_url incudes the url :port (e.g. :8080)
$host= $scheme == 'full' ? $this->config['site_url'] : $scheme . '://' . $_SERVER['HTTP_HOST'] . $this->config['base_url'];
}
if ($this->config['xhtml_urls']) {
return preg_replace("/&(?!amp;)/","&", $host . $virtualDir . $url);
} else {
return $host . $virtualDir . $url;
}
}