You have to look at the rest of the chapter that John 17:9 belongs to. You'll see that there's no conflict.
The Lord Jesus did die for the whole world, meaning everyone, so that the gift of salvation is offered to everyone.
But in John 17 the Lord prays specifically for His people (those who willingly take that gift of salvation) and fully explains why.
John 17:11-26
11 I will remain in the world no longer, but they are still in the world, and I am coming to you. Holy Father, protect them by the power of your name, the name you gave me, so that they may be one as we are one. 12 While I was with them, I protected them and kept them safe by that name you gave me. None has been lost except the one doomed to destruction so that Scripture would be fulfilled.
This passage just says that the Lord had been protecting them and kept them safe while He was with them, but now that He's coming home, He's asking His Father to protect them.
The next passage tells
why they need to be protected and shows why this prayer is only about them and not the world.
13 “I am coming to you now, but I say these things while I am still in the world, so that they may have the full measure of my joy within them. 14 I have given them your word and the world has hated them, for they are not of the world any more than I am of the world. 15 My prayer is not that you take them out of the world but that you protect them from the evil one. 16 They are not of the world, even as I am not of it. 17 Sanctify them by the truth; your word is truth. 18 As you sent me into the world, I have sent them into the world. 19 For them I sanctify myself, that they too may be truly sanctified.
From this passage we can see that the world hates God's people and that God's people don't belong to this world. They are sanctified. That's why the Lord specifically prays for His people because of the hostility against them.
But if any of the world chooses to receive the Lord's gift of salvation at any time, they too will become God's child, will be sanctified and will be hated by the world too.
20 “My prayer is not for them alone. I pray also for those who will believe in me through their message, 21 that all of them may be one, Father, just as you are in me and I am in you. May they also be in us so that the world may believe that you have sent me. 22 I have given them the glory that you gave me, that they may be one as we are one— 23 I in them and you in me—so that they may be brought to complete unity. Then the world will know that you sent me and have loved them even as you have loved me.
This part gives me goosepimples because the Lord is actually praying about us! He's wasn't just praying for the disciples with Him!
24 “Father, I want those you have given me to be with me where I am, and to see my glory, the glory you have given me because you loved me before the creation of the world.
He's praying for His people who are/were willing to receive His free gift of salvation to be with Him where He is right now!
25 “Righteous Father, though the world does not know you, I know you, and they [God's people] know that you have sent me. 26 I have made you known to them, and will continue to make you known in order that the love you have for me may be in them and that I myself may be in them.”
Again, this is why the prayer is specifically for the Lord's people. This time so that God's people will have love for the Lord in them and that He Himself may be in them (through the Holy Ghost) to live through them.
-=<>=-
So there's really no conflict between the Lord dying for the whole world and the Lord specifically praying for the protection and continued care of His people living on earth.
You can't just take one verse and then make a conclusion from that. You have to read the rest of the pertinent passages to get a better idea of what was being said and why.