A phonological rule is a formal way of expressing a systematic phonological or morphophonological process or diachronic sound change in language.  Phonological rules are commonly used in generative phonology as a notation to capture sound-related operations and computations the human brain performs when producing or comprehending spoken language.  They may use phonetic notation or distinctive features or both.

John Goldsmith (1995) defines phonological rules as mappings between two different levels of sounds representation—in this case, the abstract or underlying level and the surface level—and Bruce Hayes (2009) describes them as "generalizations" about the different ways a sound can be pronounced in different environments.  That is to say, phonological rules describe how a speaker goes from the abstract representation stored in their brain, to the actual sound they articulate when they speak.  In general, phonological rules start with the underlying representation of a sound (the phoneme that is stored in the speaker's mind) and yield the final surface form, or what the speaker actually pronounces.  When an underlying form has multiple surface forms, this is often referred to as allophony.  For example, the English plural written -s may be pronounced as [s] (in "cats"), [z] (in "cabs", "peas"), or as [əz] (in "buses"); these forms are all theorized to be stored mentally as the same -s, but the surface pronunciations are derived through a series of phonological rules.

Example 
In most dialects of American English, speakers have a process known as intervocalic alveolar flapping that changes the consonants /t/ and /d/ into a quick flap consonant ([ɾ]) in words such as "butter" () and "notable" (). The stop consonants /t/ and /d/ only become a flap in between two vowels, where the first vowel is stressed and the second is stressless.  It is common to represent phonological rules using formal rewrite rules in the most general way possible. Thus, the intervocalic alveolar flapping described above can be formalized as

Format and notation 
The rule given above for intervocalic alveolar flapping describes what sound is changed, what the sound changes to, and where the change happens (in other words, what the environment is that triggers the change).  The illustration below presents the same rule, with each of its parts labelled and described.

Taken together and read from left to right, this notation of the rule for intervocalic alveolar flapping states that any alveolar stop consonant (/t/ or /d/) becomes a tap ([ɾ]) in the environment where it is preceded by a stressed vowel and followed by an unstressed one.

Phonological rules are often written using distinctive features, which are (supposedly) natural characteristics that describe the acoustic and articulatory makeup of a sound; by selecting a particular bundle, or "matrix," of features, it is possible to represent a group of sounds that form a natural class and pattern together in phonological rules.  For example, in the rule above, rather than writing /t/ and /d/ separately, phonologists may write the features that they have in common, thus capturing the whole set of sounds that are stop consonants and are pronounced by placing the tongue against the alveolar ridge.  In the most commonly used feature system, the features to represent these sounds would be [+delayed release, +anterior, -distributed], which describe the manner of articulation and the position and shape of the tongue when pronouncing these two sounds. But rules are not always written using features; in some cases, especially when the rule applies only to a single sound, rules are written using the symbols of the International Phonetic Alphabet.

Characteristics 
Hayes (2009) lists the following characteristics that all phonological rules have in common:
Language specificity: A phonological rule that is present in one language may not be present in other languages, or even in all dialects of a given language.
Productivity: Phonological rules apply even to new words.  For example, if an English speaker is asked to pronounce the plural of the nonsense word "wug" (i.e. "wugs"), they pronounce the final s as [z], not [s], even though they have never used the word before. (This kind of test is called the wug test.)
Untaught and unconscious: Speakers apply these rules without being aware of it, and they acquire the rules early in life without any explicit teaching.
Intuitive: The rules give speakers intuitions about what words are "well-formed" or "acceptable"; if a speaker hears a word that does not conform to the language's phonological rules, the word will sound foreign or ill-formed.

Types

Phonological rules can be roughly divided into four types:
Assimilation: When a sound changes one of its features to be more similar to an adjacent sound.  This is the kind of rule that occurs in the English plural rule described above—the -s becomes voiced or voiceless depending on whether or not the preceding consonant is voiced.
Dissimilation: When a sound changes one of its features to become less similar to an adjacent sound, usually to make the two sounds more distinguishable.  This type of rule is often seen among people speaking a language that is not their native language, where the sound contrasts may be difficult.
Insertion: When an extra sound is added between two others.  This also occurs in the English plural rule: when the plural morpheme z is added to "bus," "bus-z" would be unpronounceable for most English speakers, so a short vowel (the schwa, [ə]) is inserted between [s] and the [z].
Deletion: When a sound, such as a stress-less syllable or a weak consonant, is not pronounced; for example, most American English speakers do not pronounce the [d] in "handbag".

Rule Ordering 
According to Jensen, when the application of one particular rule generates a phonological or morphological form that triggers an altogether different rule, resulting in an incorrect surface form, rule ordering is required.

Types of Rule Ordering 
Given two rules, A and  B, if we assume that both are equally valid rules, then their ordering will fall into one of the following categories:

 Feeding: the application of A creates the opportunity for B to apply.
 Bleeding: the application of A prevents B from being able to apply.
 Counterfeeding: the application of B creates the opportunity for A
 Counterbleeding: the application of B prevents A from being able to apply.

Derivations 
When a distinct order between two rules is required, a derivation must be shown. The derivation must consist of a correct application of rule ordering that proves the phonetic representation to be possible as well as a counterexample that proves, given the opposite ordering, an incorrect phonetic representation will be generated.

Example Derivation 
Below is an example of a derivation of rule ordering in Russian as presented by Jensen: Given the following rules with rule 1 applying before rule 2:

 ___# (l-Deletion)
 ___ # (Final Devoicing)

Correct Derivation: 

 /#greb+l#/ (Underlying Representation)
 greb (Application of l-Deletion)
 grep (Application of Final Devoicing)
 [grep*=] (Correct Phonetic Representation)

Incorrect Derivation: 

 /#greb+l#/ (Underlying Representation)
 ------   (Application of Final Devoicing)
 greb (Application of l-Deletion)
 *[greb] (Incorrect Phonetic Representation)

Expanded Notation 
On their own, phonological rules are intended to be comprehensive statements about sound changes in a language. However, languages are rarely uniform in the way they change these sounds. For a formal analysis, it is often required to implement notation conventions in addition to those previously introduced to account for the variety of changes that occur as simply as possible.

 Subscripts: Indicate the number of occurrences of a phoneme type.
  indicates that  or more consonants occur, where .
  indicates that  or more vowels occur, where .
 Word Boundaries: indicate the left and right boundaries that, between them contain a complete word, represented with a hashtag symbol. For example, the word "cat".
 #cat#, the beginning and end hashtags indicate the respective beginning and end of the word "cat".
 { } (Curly Braces): Indicate a logical-disjunction relationship of two expressions. For example,
 The two expressions, ABD and AED and be written with curly braces as:
 , A is followed by either B or E and then D.

 ( ) (Parenthesis): Indicate a logical-disjunction relationship of two expressions and an abbreviated version of the curly braces notation, while maintaining the same disjunctive relationship function. For example,
 The two expressions, ABD and AD and be written with parentheses as:
 , B is optionally permitted to come between A and D.
 < > (Angled Brackets): Indicate a conditional relationship within a set. For example, vowel harmony in Turkish,
 __ , All vowels will take on the [+/- back] value of the vowel that precedes it, regardless of the number of intervening consonants. If a vowel is [+ high], it will also take on the [+/- round] value of the preceding vowel, regardless of the number of intervening consonants.

Notes

References

Citations

Sources 
 Books cited

Phonology