Emperor Taizong's campaign against states of the Western Regions

Emperor Taizong of Tang (r. 626–649) of Tang dynasty China, after subjugating the Eastern Turkic Khaganate, began to exert his military power toward the Western Regions, then dominated by the Western Turkic Khaganate as well as a number of city-states loosely allied with them. In 640, Emperor Taizong sent the military commander Hou Junji to defeat and annex Gaochang—the first attempt by any Chinese dynasty to set up a permanent military and political presence in the region since Fú Jiān in the 4th Century. In 644, after Yanqi—an ally in the campaign against Gaochang—turned against Tang and allied with the Western Turkic Khaganate, Tang's commandant at Gaochang, zh, attacked and captured Yanqi's king zh, but Yanqi subsequently escaped. In 648, the ethnically Turkic Tang general zh who was the second son of Shibi Khan, attacked both Yanqi (Karasahr) and Kucha (龜茲, Qiuzi in modern Aksu Prefecture, Xinjiang), conquering both. Shule and Yutian then also submitted to Tang, allowing the dynasty to dominate the region until it was briefly seized by Tibet during the reign of Emperor Taizong's son Emperor Gaozong.

Background
In contrast to its eastern cousin the Eastern Turkic Khaganate, the Western Turkic Khaganate did not pose a major threat to the Tang dynasty in the early years of Tang's existence, as it was farther from Tang territory and not attacking Tang. Moreover, the Western Turkic Khaganate constantly suffered from internal divisions and was unable to focus itself against the Tang. Its hegemony over the Western Regions therefore remained unchallenged by Tang. In turn, both the Western Turkic Khaganate itself and its vassal-allies in the region at times nominally submitted to Tang overlordship. For example, in 619, soon after Emperor Gaozu established the dynasty in 618 and was still battling for supremacy over China with a number of rival rulers, both the Western Turkic Khaganate's Tongyehu Khan Ashina Tong and Gaochang's king Qu Boya submitted tributes to him. In 625, Ashina Tong sought marriage with a Tang princess, and Emperor Gaozu initially agreed, but the Eastern Turkic Khaganate's Illig Qaghan Ashina Duobi was displeased about the potential of a Tang-Western Turkic alliance and warned Ashina Tong against it, such that the marriage never took place.

In 626, Emperor Gaozu's son Li Shimin the Prince of Qin, who had been in an intense rivalry with his older brother Li Jiancheng the Crown Prince, ambushed and killed Li Jiancheng and another brother who supported LI Jiancheng, Li Yuanji the Prince of Qi, at Xuanwu Gate. He then effectively forced Emperor Gaozu to first create him crown prince and then pass the throne to him (as Emperor Taizong). After Emperor Taizong took the throne, in 628, Ashina Tong was killed by his uncle, Ashina Moheduo, who seized the throne (as Qulipiqie Khan). However, some of the people supported Ashina Tong's son Ashina Dieli as khan (as Siyehu Khan). Both sought aid and marriage from Tang, and Emperor Taizong declined both. Eventually, in 630, Ashina Dieli was able to defeat and kill Ashina Moheduo, again reuniting the Western Turkic Khaganate. Meanwhile, Gaochang's king Qu Wentai (Qu Boya's son and successor) made a visit to the Tang capital at Chang'an in 630-631, and was welcomed in grand ceremonies.

In 632, Ashina Dieli, who had lost the support of his people due to his cruelty and suspicions, failed in an attack against Xueyantuo, and after the failure, his own substituent tribes Shebeidaguan (設卑達官) and Nushibi attacked him and forced him to flee to Kangju, and he soon died in exile. The people supported his cousin Ashina Nishu as Duolu Khan, and Ashina Nishu nominally submitted to Tang and received Tang titles. The situation continued after he died in 634 and was succeeded by his brother Ashina Tong'e (as Dielishi Khan).

There was a further incident in 632 that contributed to the chilling of relations between Tang and Gaochang. The ancient town of Yanqi previously had a direct connection with China via a desert road, the subsequent closure of which forced Yanqi emissaries to travel through Gaochang, apparently aiding the Gaochang economy in doing so. In 632, Yanqi's king Long Tuqizhi requested that the desert road be reopened. Emperor Taizong approved of the request, leading to Gaochang resentment, and Gaochang attacked Yanqi, withdrawing only after pillaging.

By 638, it was said that Ashina Tong'e had lost the hearts of the people, and the northern half of the khanate supported an Eastern Turkic prince, Ashina Yugu as Yipiduolu Khan. There were major battles between Ashina Tong'e and Ashina Yugu, but neither side was able to prevail, and therefore divided the khanate in two, with the Ili River serving as their boundary. The division continued after Ashina Tong'e died in 639 and was succeeded by his nephew Ashina Bobu (as Shaboluoyehu Khan).

Campaign against Gaochang
Meanwhile, Gaochang remained hostile to both Yanqi and Tang, and also in 638, Gaochang, allied with the Chuyue (處月) and Chumi (處密) tribes, attacked Yanqi, capturing five Yanqi cities and 1,500 Yanqi men and women before retreating. Qu Wentai also entered into an alliance with Ashina Bobu against a Tang ally, Yiwu (伊吾), in modern Hami Prefecture, Xinjiang), as well as Yanqi. In 639, Emperor Taizong issued an edict rebuking Qu Wentai and ordering him to send his official Ashian Ju (阿史那矩) to Tang in order to discuss Tang-Gaochang relations; Qu Wentai refused to send Ashina Ju, but instead sent another official, Qu Yong (麴雍), to apologize.  Emperor Taizong also ordered Qu Wentai to turn over the Chinese who were previously in exile in the Eastern Turkic Khaganate who had fled to Gaochang when Tang conquered the Eastern Turkic Khaganate in 630.  Qu Wentai further incensed Emperor Taizong by trying to persuade Xueyantuo's Zhenzhu Khan Yi'nan to take a more independent stance from Tang.  Emperor Taizong began planning an invasion against Gaochang, and Yi'nan offered to assist, although historical records did not indicate any actual Xueyantuo participation.

Around the new year 640, after Emperor Taizong's last attempt to get Qu Wentai to change his anti-Tang stance failed, Emperor Taizong commissioned the general Hou Junji to command an army, assisted by the general Xue Wanjun (薛萬均), to attack Gaochang. Qu Wentai initially did not take the threat seriously—believing that his kingdom was able to withstand an attack from a small Tang army and that Tang could not launch a large army due to logistics issues of marching through the desert. However, by fall 640, Hou had gotten his army through the desert, almost at Gaochang, and Qu Wentai, hearing this, died in anxiety, and was succeeded by his son Qu Zhisheng (麴智盛). Hou, rejecting a proposal to make a surprise attack against Qu Wentai's funeral procession, first attacked and captured Tiandi (田地), just east of Gaochang's capital. He then marched on Gaochang itself. Qu Zhisheng wrote him to apologize for his father's offenses, and Hou ordered him to surrender, which Qu Zhisheng refused. Hou put the city under siege, but Ashina Bobu, instead of aiding Gaochang as he promised, withdrew far from Gaochang, while a general that he had sent to defend Kehanfutu (可汗浮圖, in modern Changji Hui Autonomous Prefecture, Xinjiang), in fear, surrendered to Hou. With no Western Turkic aid coming, Qu Zhisheng surrendered. Emperor Taizong's chancellor Wei Zheng advised allowing Gaochang to remain as a vassal, with Qu Zhisheng continuing to serve as king, but Emperor Taizong decided against doing so, instead annexing Gaochang territory and creating two prefectures—Xi Prefecture (西州, headquartered at Gaochang) and Ting Prefecture (庭州, headquartered at Kehanfutu). He furthered established the Protectorate General to Pacify the West at the fortress of Jiaohe (交河, near Gaochang as well) to keep a military presence. Hou took Qu Zhisheng and his officials back to Chang'an, while returning several cities that Gaochang had previously seized from Yanqi back to Yanqi. Emperor Taizong created Qu Zhisheng the Duke of Jincheng and kept him as a general at Chang'an. Emperor Taizong tried to strengthen Xi Prefecture's defense by commuting the condemned prisoners' death penalties and instead exiling them to Xi Prefecture, while conscripting people originally sentenced to exile into the army defending Xi Prefecture, with a term of service commensurate with the length of exile they were originally sentenced.

Between the campaigns against Gaochang and Yanqi
Meanwhile, Ashian Bobu, who was under attack by Ashina Yugu, formally submitted to Tang in 641, but the submission did not help him in his campaign with Ashina Yugu, and later in 641, one of Ashina Yugu's generals captured Ashina Bobu, and Ashina Yugu executed Ashina Bobu, reuniting the divided the Western Turkic Khaganate. After Ashian Yugu then conquered Tuhuoluo (吐火羅, may be the same people as Tocharians), he attacked Yiwu in 642, which had by now been converted into Tang's Yi Prefecture (伊州), although his attacks were repelled by the Tang general Guo Xiaoke (郭孝恪). At the same time, Ashina Yugu began to himself suffer dissent within, as he was said to have hoarded the spoils from attacks on Kangju and Mi (米, a state on the Amu Darya) and refused to divide them with his subordinates—and when one of his generals, Ashina Nishou (阿史那泥熟) nevertheless seized some, Ashina Yugu executed him, causing Ashina Nishou's subordinate Huluwu (胡祿屋) to rebel. The rebels sought aid from Tang, and Emperor Taizong created Ashina Moheduo's son as Yipishekui Khan. Ashina Yugu initially prevailed in battle against Yipishekui Khan, but the rebels refused to submit despite the defeats, and Ashina Yugu eventually withdrew and took up position in former Tuhuoluo territory. However, for the next several years, the Western Turkic Khaganate appeared to be in a decentralized state.

Campaign against Yanqi
Meanwhile, Yanqi had remained friendly with Tang, but in or before 644, the Western Turkic general Ashina Quli (阿史那屈利) had taken the Yanqi king Long Tuqizhi's daughter to be his brother's wife, and, in response, Long Tuqizhi began to favor the Western Turkic Khaganate and decreased its tributes to Tang. Guo Xiaoke requested permission to attack Yanqi, and in 644 Emperor Taizong approved. It happened at the time that three of Long Tuqizhi's brothers were at Xi Prefecture, and Guo made one of them, Long Lipozhun (龍栗婆準) his guide. It was said that because Yanqi's capital was surrounded by water, it took little precautions against an attack, and Guo made a surprise attack, crossing the waters at night. In the morning, the Tang soldiers climbed the walls of Yanqi, capturing it and Long Tuqizhi. Guo put Long Lipozhun in charge of Yanqi's affairs and withdrew. Three days later, Ashina Quli arrived with a relief force, but Guo had already withdrawn; he therefore seized Long Lipozhun and chased after Guo, who struck back and stopped his advance.

Another Western Turkic general, Ashina Chuna (阿史那處那), made one of his subordinates, a tudun, the protector general over Yanqi. The protector general, in the stead of the king of Yanqi, thereafter submitted tributes to Tang. But when his emissary arrived in Chang'an, Emperor Taizong rebuked him and stated, "I attacked Yanqi. Who are you to occupy it?" In fear, the Western Turkic protector general abandoned Yanqi, and the Yanqi nobles supported Long Lipozhun's cousin Long Xuepoanazhi (龍薛婆阿那支) to be the new king, but continued to be submissive to Ashina Chuna. Meanwhile, Long Tuqizhi and his family were delivered to Chang'an, where Emperor Taizong released them.

In 646, the Yipishekui Khan offered tributes to Tang and requested to marry a Tang princess. Emperor Taizong agreed—but ordered him to submit, as dowry, five states that were the Western Turkic Khaganate's vassals, Kucha, Yutian, Shule, Zhujupo (朱俱波, in modern Kashgar, Xinjiang) and Congling. There is no historical record of what the Yipishekui Khan's response was, but no marriage ever took place.

Campaign against Yanqi and Kucha
In or sometime before 647, Kucha's king Suvarnadeva (Sufadie 蘇伐疊) died, and was succeeded by his brother Haripushpa (Helibushibi 訶黎布失畢). Haripushpa decreased his tributes to Tang and also attacked neighboring states submissive to Tang. Emperor Taizong became angered, and around the new year 648, he commissioned Ashina She'er, a general who was formerly an Eastern Turkic prince, as the commander of an army to attack Kucha, with the generals Qibi Heli (契苾何力) and Guo Xiaoke assisting Ashina She'er, and also requisitioned soldiers from the Tiele tribes, the Eastern Turkic people, Tibet, and Tuyuhun. In fall 648, Ashina She'er first advanced into the Dzungarian Basin and attacked the Chuyue (possibly Chigil) and Chumi tribes, forcing them to surrender. The Tang expedition then struck south to enter the Tarim Basin between Kucha and Yanqi. Intimidated by the Tang advance, Long Xuepoanazhi abandoned Yanqi and took up position in eastern Kucha, but Ashina She'er captured and executed him. Ashina She'er made Long Xuepoanazhi's cousin Long Xiannazhun (龍先那準) the king of Yanqi.

Ashina She'er then marched on Kucha. Haripushpa sent his chancellors Nali (那利) and Jieliedian (羯獵顛) to resist Ashina She'er's forward commander Han Wei (韓威). Once engagement started, Han pretended to retreat, and when Kucha forces pursued, Han and the secondary commander Cao Jishu (曹繼叔) counterattacked and defeated them. Haripushpa took up position within Kucha's capital Yiluolu (伊邏盧). Ashina She'er attacked Yiluolu, and Haripushpa fled. Ashina She'er had Guo take up position at Yiluolu and continued pursuing Haripushpa, who by this point had fled to Bohuan (撥換, also in modern Aksu). Ashina She'er besieged it for 40 days and captured it, taking Haripushpa and Jieliedian, but Nali escaped and led remaining Kucha troops and relief forces from Western Turkic to attack Yiluolu, catching Guo by surprise and killing him in battle. However, after battling both in and outside the city, Nali could not control Yiluolu and was forced to flee. The people of Kucha then captured Nali and delivered him to Ashina She'er. Ashina She'er made a younger brother of Haripushpa the new king, and after receiving tributes from the Western Turkic Khaganate, Yutian (Khotan), and An (安, Bukhara), withdrew.

Aftermath
Emperor Taizong died in 649. Subsequent to his death, a Western Turkic prince that he had supported, Shabolüe Khan Ashina Helu (阿史那賀魯), defeated and killed the Yipishekui Khan, taking over Western Turkic, but subsequently broke away from Tang and attacked Tang territory. Emperor Taizong's son and successor Emperor Gaozong launched two campaigns against Ashina Helu. The first, launched in 655 and commanded by Cheng Zhijie (程知節), ended in failure, as it was forced to withdrew when supplies ran out. The second, launched in 657 and commanded by Su Dingfang, was a thorough victory, as Tang forces captured Ashina Helu and put Western Turkic territory under the control of two Western Turkic princes submissive to Tang, the Xinxiwang Khan Ashina Mishe (阿史那彌射) and the Jiwangjue Khan Ashina Buzhen (阿史那步真), becoming the dominant power in the region.

The Tang established the Four Garrisons of Anxi in the Tarim Basin after defeating Ashina Helu's subordinate Duman at Kasghar in 659. The Tibetans invaded the Tarim Basin in the 660s and drove Tang forces out in 670. A Tang counter-attack regained the Tarim Basin in 692. The Tang then maintained control over the Western Regions for another century, but the loss of the Gansu Corridor to the Tibetans after the An Lushan rebellion caused the Four Garrisons to be cut off from the Tang empire and finally lost to the Tibetans for the second and last time in the 790s.