I'm writing this book series called the Weasel Sagas (link here for more on that) that has this planet called Aurea. Aurea is the planet in this universe most similar geographically to Earth, with forests, grasslands, plains, jungles, and deserts. The civilization here (which is based on the Byzantine Empire and has similar culture/tech) has some cities (usually along rivers or near oases) in a vast desert that is home to colossal Dune-style sandworms. These sandworms can grow to be over 1,000 feet long, weigh several hundred tons, and be around 100 feet in diameter. These worms are carnivorous (normally feasting on the Landsharks, Camels, Zuniceratops, Hippogriffs, and Deinocherius native to this large desert) and will attack humans if there are enough of them in one place to pique their interest (i.e. a city setting or an army marching through the desert). These sandworms, although fairly squishy on the inside, are protected by a thick exoskeleton almost as strong as kevlar (although very small areas around their mouths and nostrils are unprotected for obvious reasons). Although they are blind, they can smell large concentrations of prey from up to 50 miles away and can slither through sand at up to 60 mph. Getting through stone is tougher, but they can accomplish this at speeds of around 30 mph by gnawing through the sandstone underneath the desert (some of which ends up in its gizzard to assist digestion). However, these beasts are not powerful enough to drill through solid bedrock. How would these cities go about defending themselves from these beasts? What weapons would be best to use? What tactics? How much damage would these cities suffer in the event of an attack? I'll include more info on the main two cities in question here (just as a side note, these maps tell of these cities AFTER mass industrialization, so ignore the spaceports):
Best thing they can do for cities is build on a place the sandworms can't dig through. There really isn't anything a medieval army can do against those worms to defend their city. They can tunnel beneath defenses, so yeah... Not to mention even if they surface medieval weapons aren't going to do anything to that thing. Perhaps stuff like boilling oil or ballista, but those things aren't exactly easy to set up nor cheap or very accurate.
I would imagine buckets of water and other tactics to get a reading on tunneling would be well spread out to get some sort of early warning that a worm will surface.
I think the best defense would be to pevent these worms from getting too close to the cities or trading routes to smell the people. Ensure that cattle is 'sacrificed' near the border to keep the worms in the areas you want them to stay in. The worms won't have a reason to go searching any farther for their food if their food is delivered to them.
If a worm gets in a city it's basically going to be the medieval variant of Godzilla going ham in Tokyo. Only thing people can hope for is that they're not the ones going to be eaten and that it doesn't take too long for the worm to kill sufficient other people to leave again. One worm rampage could send a medieval city in a recession from sheer damage.
Sacrificing cattle every so often is probably the most effective and cheapest solution.
In Dune people lived on rock, because the worms ruled the sand. If you think of sand as the oceans and rock as the continents, the depth of the sand is also important: maybe the worms can't come into the shallow areas near exposed rock, and can only travel through the deep sand further out. But if you're giving the worms the ability to eat through solid rock, then it's nothing like Dune and we need an entirely different approach.
As an aside, assuming these sandworms are an older species that has existed from time before time, then certainly there wouldn't be any rock, only sand and gravel, by the time humans evolved.
A solution for your cities would involve understanding why the whole world isn't desert. In Dune the worms were allergic to water (or something... it's been a while). So perhaps your world only has worms where there's no water? Anywhere with a decent amount of moisture, enough to grow plants, there won't be any worms. Even in the desert, cities could grow in river valleys (think Egypt) and oases.
The big question is, why would humans want to go into a desert that has no moisture at all and no rocks. This wouldn't be like the Arizona desert that has scenery, wildlife, and valuable minerals -- it'd be a barren sandbox. The only reason I can think of is that they might want to cross the desert for trade, if that would be the shortest route from point A to point B.
Let's go with that. If humans want to create new trade routes across the desert, and water is a defense against the worms, they might build canals from point A to point B and roads alongside the canals.
You worms hunt mostly by smell it seems, so the humans would position their cities in such as to drive their smell away from the areas the worms inhabit, being near current water would help with that.
Some cities could build giant canals around the city since the worms don't seem capable of crossing water all that well.
If a worm is seen near the city, then giant incense pyres would be lit to mask the human smell. The humans would probably start looking for aroma combinations that could drive the worms away.
You built like in Tenochtitlan (Mexico city)
Your cities aren't "usually along rivers or near oases", but in the middle of rivers, lakes, swamps, delta areas. People travel in boats instead of carriages. The ground is soft, and when the sandworm attacks, it needs to swallow a full lake. Even if they aren't allergic to water, the sudden stream of water to their mouths and the tunnel collapsing from losing the integrity, means a predator would choose another prey.
Your sandworms are super-predators, with that size they need to be sleeping for long times (to preserve energy) and then only move to eat, because humans are to them like peas to us. So they will be oportunistic hunters, eating everything that is easy to catch.
It's the only defense I can think of, because the Medieval weapons won't make anything to the sandworms, they are too massive. If you have enough time to develop a civilization (Byzantine Empire level needs a lot of people to develop technologies), with the time you can hunt and almost drive to extinction the sandworms (as we have done with so many wild animals on Earth). Maybe with poisoned bait. Or killing their prey and hoping they turn into cannibalism.
I feel like it's a given that they're pretty screwed if the worms get into their cities, and the fact that they can crunch up bedrock makes them fairly OP, but have you considered a subterranean blade system, or something biological or chemical that hurts them?
They had a good bit of steel during Byzantine times, and it's likely that even if the worms can crush up bedrock and move through sand in a way that seems fluid, that it would still be harmed by ingesting poisons and sharpened blades. What's more, if the people have a sense that the worms are attracted to them, perhaps there can be a situation where they set traps to lure the monsters towards people(even if they don't know it's smell, I could envision them using undesirables as bait to satiate the terrifying beasts).
Interestingly, during the early Byzantine Empire, there was a huge anti-pagan sentiment, especially insofar as they used sacrifices and that could be a fun dynamic where the pagans have a better sense of the worms because they sacrifice people/creatures to the beasts in an effort to remain favored by them.
The opportunity exists that the pagans could use their sacrifice as a type of bait and either trap the worms (ludicrous, but awesome), or they could at least set a trap that harms them. Alternatively, the pagan tradition could be abused by a non-pagan in order to harm the worms and it could be the thing that causes problems. Just some ideas that I had while mulling it over.