---
title: "ルールベースの構成を使用"
slug: "use-a-rule-based-configuration"
abstract: "ルールベースロジックを使って、どの条件下で設定設定が適用されるかを指定する方法。"
---
# ルールベースの構成を使用

パッチファイルを使ってSitecoreの設定をカスタマイズすると、ルールベースのロジックを使って特定の設定が適用される条件を指定できます。

ルールベースの構成はweb.configファイル内で設定されたルール定義を使用します。Sitecoreには2つのあらかじめ定義されたルール定義があります。 *role*はインスタンスが実行 [するSitecore](https://doc.sitecore.com/xp/ja/developers/93/platform-administration-and-architecture/define-the-server-role.html "Define the server role")ロールを決定し、*search*は最適化される検索プロバイダーを決定Sitecore。また、独自の [カスタムルール定義](https://doc.sitecore.com/xp/ja/developers/93/platform-administration-and-architecture/add-a-custom-rule-to-your-configuration.html "Add a custom rule to your configuration")を追加することもできます。

このトピックでは、以下の方法を説明します:

-   [設定設定をルールに依存してください](https://doc.sitecore.com/xp/ja/developers/93/platform-administration-and-architecture/use-a-rule-based-configuration.html#UUID-55a5e853-c450-df0a-c901-abf08361f6ae_id__Make_configuration_settings "Make configuration settings dependent on a rule")
-   [同じ設定で2つ以上のルールを使う](https://doc.sitecore.com/xp/ja/developers/93/platform-administration-and-architecture/use-a-rule-based-configuration.html#UUID-55a5e853-c450-df0a-c901-abf08361f6ae_id__Use_two_or "Use two or more rules for the same setting")

## 設定設定をルールに依存してください

設定設定をルールに依存させるには:

1.  ルールがweb.configファイルに定義されていることを確認してください。 [それはあらかじめ定義されたルール](https://doc.sitecore.com/xp/ja/developers/93/platform-administration-and-architecture/rule-based-configuration.html "Rule-based configuration") でもカスタム [ルール](https://doc.sitecore.com/xp/ja/developers/93/platform-administration-and-architecture/add-a-custom-rule-to-your-configuration.html "Add a custom rule to your configuration")でも構いません。
    
2.  実装したい設定をパッチ [ファイル](https://doc.sitecore.com/xp/ja/developers/93/platform-administration-and-architecture/use-a-patch-file-to-customize-the-sitecore-configuration.html "Use a patch file to customize the Sitecore configuration") にまとめて作成してください。
    
3.  パッチファイルの<configuration>文に「Old xmlns:rulename="http://www.sitecore.net/xmlconfig/rulename/"」の文を追加してルールの名前空間を登録します。
    
    > !注あらかじめ定義されたルール定義の役割を使い、パッチファイルで検索する場合は、それらの名前空間も登録しなければなりません。
    
    例えば、以下はlocalenvというルール定義のための名前空間を追加します:
    
    <configuration xmlns:patch=http://www.sitecore.net/xmlconfig/ xmlns:localenv="http://www.sitecore.net/xmlconfig/localenv/">
    
4.  単一の設定をルールの値に依存させるには、その設定を定義する文に構文rulename:require=testvalueのテストを追加します。
    
    例えば、次の はSearch.ClassicSearch.MaxResults6に を定めます。localenvがQAの値を含む場合:
    
    <setting name="Search.ClassicSearch.MaxResults" localenv:require="QA">
    <patch:attribute name="value">6</patch:attribute>
    </setting>
    
5.  パッチファイル内のすべての設定をルールの値に依存させるには、パッチファイルの <sitecore> 文に構文rulename:require=testvalueのテストを追加します。例えば、<sitecore>文に含まれるすべての設定が、QAlocalenvを含む場合に適用されます:
    
    <sitecore localenv:require="QA">
    …
    </sitecore>
    

> !注localenvが複数の値を含む場合、localenv:require="QA" の文は、そのうちの1つの値がQAである限り真です。文はルール定義に含まれるすべての値に一致する必要はありません。

テストは複数のテスト値を含み、演算子and or ! (not)を使用できます。以下の表は有効なルールテストの例を示しています。

ルールテスト

結果

localenv:require="QA"

localenv が を含む場合に設定が適用QA。

localenv:require="!QA"

localenv に QAを含まない場合、設定が適用されます。

localenv:require="QA or Test"

設定は localenv が QA または Test 、または両方を含む場合に適用されます。

localenv:require="QA and Test"

localenv が QA と Testの両方を含む場合に設定が適用されます。

localenv:require="QA and !Test"

localenv が QA を含むが Testを含まない場合、この設定が適用されます。例えば、localenv が QA,Productionの場合に適用されますが、QA,Testの場合は適用されません。

localenv:require="(QA or Production) and !Test"

localenv が QA または Production を含むが Testを含まない場合、この設定が適用されます。例えば、localenv が QA,Developmentの場合は適用されませんが、QA,Testの場合は適用されません。

## 同じ設定で2つ以上のルールを使う

同じ設定設定に複数のルールのテストを追加できます。

同じ設定で2つ以上のルールを使う場合:

-   個々の設定の文や <sitecore> 文に必要なすべてのテストを追加します。すべてのテストが合格すればSitecore設定を適用します。
    
    次の例では、Sitecore localenvがQAの値を持ち、roleがContentManagementまたはContentDeliveryのどちらかの値を含んでいる場合に設定を適用します。設定を適用するには両方のテストに合格する必要があります:
    
    <sitecore localenv:require="QA" role:require="ContentManagement or ContentDelivery">
    

テストは常にandを用いて連結されます。同じ文で複数のテストを連結する際にはor or notを使うことはできません。各テスト内ではandやorを使わずに使えます。例えば、次の文は有効です:

<sitecore localenv:require="QA and !Test" role:require="Standalone or ContentDelivery">

> !注[Show Configツール](https://doc.sitecore.com/xp/ja/developers/93/platform-administration-and-architecture/view-configuration-changes.html "View configuration changes") を使って、Sitecoreが実行時にコンパイルした際の結果として見た仕上がりを確認できます。また、Show Configツールを使って、実際に設定を変更しずに設定ルールの変更や設定レイヤーの無効化をシミュレートすることもできます。