http://dwarffortresswiki.org/index.php/DF2012:Security_design#Entrance_designs----
Building doors and manually forbidding them as appropriate is the easiest method (press q then move the cursor to the door then press lowercase L).
Hatches work too, with
potential added protection from
building destroyers.
----
You can also use a bridge attached to a lever. The typical method is to have a bridge set to raise in line with the hallway, so that when it's up, it forms a wall. Pull the lever to on to raise the bridge and close the hallway. Pull it to off to lower the bridge and open the hallway. If the bridge is built over solid land,
closing the bridge on top of things is dangerous. Doors, floodgates, hatches and such can also be linked to levers.
----
Using bridges as paths is pretty powerful. Two hallways on different z-levels. There are ramps leading to the lower hallway. When the bridge is down, it covers the ramps leading to the lower hall, closing it, and at the same time unblocking the upper hall. When the bridge is up, the upper hall is inaccessible, making the lower hall the only path.
Bridge down
....__....
\.....Bridge up
.... |...
\.....(Bridge is marked in
red.)
See also:
Dwarven airlock for some added design concepts on how to control multiple parts of the paths with one lever pull.
----
Another option is to have a second path that's longer, and designate the trapped path as "restricted" (press d o r then select as usual.) Although the dwarves will still attempt to up the goblinite and so forth, unless you manage that somehow (such as with o F, but that can add more micromanagement than it fixes.) The safe hallway can even be 3-wide so that caravan wagons can get through, but the stupid guards and outpost liasons will still get themselves spiked to death.
In short, if the path is marked as restricted, the cost is 25. If the path is normal, the cost is 2. Simple math, (25 / 2) is 12.5. So, if the longer safe path is under 12.5 times the length of the short trapped hallway, they will take the longer path.
To give an example, if the short trapped hallway is 10 tiles long, and the longer safe hallway is 124 tiles or less, they will take the longer hallway. But if the longer hallway is 126 tiles or more, they will opt for the trapped hallway. To simplify the math, I'm assuming the paths start and end at the same spot. But obviously, you have a huge fudge factor, so you don't even need to do the math since you'll usually have something like a 10 long trapped hallway and a 30 long safe hallway.