以下の設定でCraft 3マルチサイトをセットアップしました:
しかし、私が本当に欲しいのは、
また、「デフォルト」の http:// localhost:8888
を設定したいのでブラウザの言語に基づいて/ enまたは/
esにいつでもリダイレクトすることができますが、管理パネルからCMSのサイトとして追加したくありません。各エントリのバージョン。
どちらが行くの?
これは3つの主要なステップで行うことができます。
1。サイトのサブディレクトリを取得する
最初に/en/
サブディレクトリと/es/
サブディレクトリが機能するようにしてください:
- Create
en/
andes/
subfolders in your
webroot - Each subfolder should have its own
index.php
file
and.htaccess
file (if using Apache). -
Assuming you used the craftcms/craft project as a starting point, update
thedefine('CRAFT_BASE_PATH'...
line in each of the
index.php
files to:define('CRAFT_BASE_PATH', dirname(__DIR__, 2));
-
Update your English site’s Base URL to include the
/en/
.
この時点で、ブラウザが/en/
に直接向いている場合、英語のサイトは期待通りに動作するはずです。
2。ウェブルートのindex.phpにリダイレクトスクリプトを作成する
Webルートに( en/
と es/
サブフォルダの横に)
index.php
ファイルを作成します。このスクリプトは、要求されたURIと言語に基づいて、要求を正しいサブフォルダにリダイレクトする責任があります。
<?php
// Bootstrap Craft -------------------------------------------------------------
// Set path constants
define('CRAFT_BASE_PATH', dirname(__DIR__));
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');
// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';
// Load dotenv?
if (file_exists(CRAFT_BASE_PATH.'/.env')) {
(new DotenvDotenv(CRAFT_BASE_PATH))->load();
}
// Load Craft (but don't run)
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
/** @var craftwebApplication $app */
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
// Custom Logic ----------------------------------------------------------------
// find a site language that matches the browser's requested language
$siteLanguages = $app->i18n->getSiteLocaleIds();
$language = $app->request->getPreferredLanguage($siteLanguages);
// find the first site that uses that language
foreach ($app->sites->getAllSites() as $site) {
if ($site->language === $language) {
//found it
break;
}
}
// figure out where to redirect this request now
$url = (function() use ($app, $site) {
$uri = $app->request->getPathInfo();
//see if we have an exact URI/language match
if ($element = $app->elements->getElementByUri($uri, $site->id, true)) {
return $element->getUrl();
}
//see if we have an exact URI match in any other site
foreach ($app->sites->getAllSites() as $otherSite) {
if ($otherSite !== $site) {
if ($otherSiteElement = $app->elements->getElementByUri($uri, $otherSite->id, true)) {
//found one! now see if the element is also available in the requested site
if ($element = $otherSiteElement::find()->id($otherSiteElement->id)->siteId($site->id)->one()) {
return $element->getUrl();
}
//otherwise we'll just ignore the browser language
return $otherSiteElement->getUrl();
}
}
}
//no element matches. we'll just redirect to the same URI in the requested site.
return crafthelpersUrlHelper::siteUrl($uri, null, null, $site->id);
})();
// redirect to the URL
$app->response->redirect($url)->send();
これで、ブラウザを /index.php?p=some/uri
にすることで、リダイレクトのテストを開始できるはずです。
3。非ローカライズされたリクエストをウェブルートのindex.phpにリダイレクトする
最後に、 en/
または es/
で始まらないリクエストをすべて
index.php
ファイルにあなたのWebルート。 Apacheを使用している場合は、
mod_rewrite
を使用して行うことができます。
# Send requests missing en/ or es/ to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(en|es) [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
今はすべてがうまくいくはずです!