Glasgow Warriors ended their stay at Firhill by beating Connacht for the first time in five meetings to secure a play-off semi-final against Leinster.
Two Duncan Weir penalties to one by Miah Nikora had edged Glasgow ahead before DTH van der Merwe completed a marvellous move for a Warriors try.
Weir took his penalty tally to four as Sean Lineen's men stretched their lead.
And a John Barclay try from a driven scrum 10 minutes from time rewarded the hosts for a tenacious performance.
The win, coupled with Munster's 36-8 hammering of Ulster, means Glasgow finish fourth in the Pro 12 table and face table-topping Leinster away. Connacht finished eighth in the league.
Lineen had made seven changes to the Warriors line-up.
Van der Merwe started, while scrum-half Henry Pyrgos, winger Federico Aramburu, Jon Welsh, Dougie Hall, Moray Low and Tom Ryder came in.
For Connacht, Adrian Flavin and Johnny O'Connor took over from Ethienne Reynecke and Ray Ofisa in the pack with centre Eoin Griffin replacing Fetu'u Vainikolo.
Weir got the scoreboard ticking over when he kicked a penalty through the posts for Glasgow, but only after missing a much harder penalty from almost the halfway line in the third minute.
But within moments the referee penalised the hosts for not releasing after the tackle and Nikora had a simple task to restore parity.
Connacht picked up the pace and attacked in waves, with Weir making a vital defensive tackle, Nikora jinking through the Glasgow ranks before an inopportune slip and Pyrgos hoofing clear under pressure.
The Irish were penalised for offside midway through the first half, serving Weir with another chance to score from long-range. His confident kick gave Lineen's side a three-point advantage.
The first try was roared to the rafters by the home crowd as Van der Merwe rounded off a wonderful move, with Graeme Morrison, Richie Gray and Stuart Hogg combining from right to left to set the South African-born Canadian free 10m from the line.
Weir, though, sliced the conversion but left Glasgow with an 11-3 lead over the Irish province.
Captaining Warriors for the night was the Argentine Aramburu, who will retire at the season's end, and his foray deep into the Connacht half kicked off a melee as the interval approached, the expected physical encounter turning nasty for a moment.
When the dust settled, Weir signalled to the Glasgow crowd for extra encouragement and the 5,300 fans gave a zestful response, many clearly not sated after attending the IRB Sevens World Series at Warriors' new home, Scotstoun.
Glasgow number 10 Weir added to the score with a straightforward penalty after 48 minutes.
Hogg found touch with a monstrous kick from his own 22 almost to put pile more pressure on the visitors.
And stemming from that set-piece Van der Merwe, not long back from shoulder surgery, thrilled the fans with a slalom through half a dozen green shirts down the left before being barged out of play by Nikora.
Weir's final contribution before being replaced by Ruaridh Jackson was to strike his fourth penalty of the match to make it 17-3 with 20 minutes remaining.
Glasgow were in control but Connacht, unbeaten against Glasgow in four games, continued to make progress difficult.
The result was put beyond doubt when Paul O'Donohoe was sin-binned and during his absence the hosts drove a scrum from about five metres over the line - Barclay the man to land on the ball, Jackson adding the extras.
Connacht tried a similar tactic with the scrum with only a few minutes remaining and claimed a try but the TMO ruled that the ball had not been grounded.
Moments earlier Barclay was sin-binned for repeatedly ignoring the referee's instructions.
Defiant Connacht fought until the end and had the upper hand before the final whistle brought about celebrations to seal Glasgow's time at the Maryhill stadium.
Glasgow: Hogg, Aramburu, Dunbar, Morrison, van der Merwe, Weir, Pyrgos, Welsh, Hall, Low, Ryder, Gray, Harley, Fusaro, Barclay.
Replacements: Shaw for van der Merwe (60), Jackson for Weir (60), Cusiter for Pyrgos (60), R Grant for Welsh (51), MacArthur for Hall (51), Cusack for Low (60), Kellock for Gray (60), Forrester for Fusaro (68).
Connacht: Duffy, Tonetti, Griffin, Fa'afili, O'Halloran, Nikora, F Murphy, Buckley, Flavin, Loughney, Swift, McCarthy, Muldoon, J O'Connor, Naoupu.
Replacements: Loxton for O'Halloran (56), Jarvis for Nikora (56), O'Donohoe for F Murphy (62), Ah You for Buckley (51), D Murphy for Flavin (70), Maguire for Loughney (65), Kearney for Swift (65), Ofisa for J O'Connor (54).
Referee: Peter Fitzgibbon (Ireland).