> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom rules

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic explains how to use custom rules to target contexts based on their attributes.

## Target contexts with custom rules

You can create custom targeting rules using any context kinds and any context attributes.

To create a custom targeting rule:

1. In the flags dashboard, scroll or search to find the flag you want.
2. Click the flag's name to open the flag's **Targeting** tab.
3. If the flag is off and the rules are hidden, click **View targeting rules**.
4. Click the **+** button between existing rules, and select "Build a custom rule":

<Frame caption="The &#x22;+&#x22; menu, with targeting options.">
  <img src="https://mintcdn.com/launchdarkly/A4UXoRTW8ATNw4yq/images/auto/targeting-tab-add-rule.auto.png?fit=max&auto=format&n=A4UXoRTW8ATNw4yq&q=85&s=d27d3a89b1f74b12bf6493d3696381f0" alt="The &#x22;+&#x22; menu, with targeting options." width="440" height="482" data-path="images/auto/targeting-tab-add-rule.auto.png" />
</Frame>

5. (Optional) Enter a name for the rule.
6. Select an option from the **Context kind** menu:

* Choosing a specific context kind lets you target on attributes for contexts of that kind.
* Choosing "Context kind" lets you target one or more context kinds. If you choose "Context kind," skip to step 5.
* Context kinds are included in this menu after either you [create them from the **Contexts** list](/docs/home/flags/context-kinds-create#create-context-kinds) or [evaluate a context using a LaunchDarkly SDK](/docs/sdk/features/context-config).

7. In the **Attribute** menu, select one of this context's attributes.
8. In the **Operator** menu, select the operator for your clause.
9. In the **Values** menu, enter one or more values to check against.
10. (Optional) Click the **+** button to add additional clauses to your targeting rule.
11. From the **Select...** menu, choose a rollout option:
    * a single variation to serve
    * a manual [percentage rollout](/docs/home/releases/percentage-rollouts)
    * a [progressive rollout](/docs/home/releases/progressive-rollouts)
    * a [guarded rollout](/docs/home/releases/guarded-rollouts)
    * an [experiment](/docs/home/experimentation)
12. Click **Review and save**. The "Save changes" dialog appears.
13. Review your targeting changes, [add comments](/docs/home/releases/comments), or [request approval](/docs/home/releases/approvals) as needed.
14. (Optional) Click **Test run** to preview the percentage of contexts that will receive each flag variation.
15. Click **Save changes**.

<Note>
  **Custom targeting rules can include mobile application data**

  If you are using a mobile SDK that supports [environment attributes](/docs/sdk/features/environment-attributes), you can target your mobile applications based on details about your application, application version, and device using a [mobile targeting rule](/docs/home/flags/mobile-targeting). To create custom targeting rules using this same context information, use the `ld_application` and `ld_device` context kinds.

  If you are using other mobile SDKs, you can target your mobile applications using any context attributes that you have created.
</Note>

If a targeting rule references any context kinds or attributes with `null` values, or that do not exist for a given context, then the flag skips that rule. For example, in a rule that checks "region is one of Canada," any context whose region attribute is not set or is set to `null` does not match the rule. Similarly, in a rule that checks "region is not one of Canada," any context whose region attribute is not set or is set to `null` does not match the rule. This behavior ensures that your rules only target contexts for which you explicitly have attribute information.

### Multiple conditions and values

You can add multiple conditions to a rule. Here is how rules handle multiple conditions and values:

* Contexts must meet *all* the conditions in a rule to match the rule. If any of the conditions are not met, the context will not match the rule.
* If a condition has multiple values, LaunchDarkly considers the condition met if there is a match on *any* of the values.
* If an attribute has an array value, LaunchDarkly treats it as multiple values and allows any of the values within the array to match a rule.

For segment-based conditions, selecting multiple segments in a single condition uses OR logic. The condition matches if the context is a member of any one of the selected segments.

To reference a rule when working with other members of your organization, click the **three-dot** overflow menu and choose **Copy link to rule**.

<Card icon="https://mintcdn.com/launchdarkly/YIC2H8XW-fomhquw/assets/icons/openapi-logo.svg?fit=max&auto=format&n=YIC2H8XW-fomhquw&q=85&s=dd9578a9668a86e1a8b8c314921f204c" horizontal width="2500" height="2452" data-path="assets/icons/openapi-logo.svg">
  You can also use the REST API: [Update feature flag](/docs/api/feature-flags/update-feature-flag)
</Card>
