Preheat the oven to 350°F.
Bring a large pot of salted water to a boil. Once boiling, add the pasta and cook according to package directions for al dente. Drain the pasta (do not rinse) and return it to the pot.
Meanwhile, heat the olive oil in a large skillet over medium heat. Add the onion and cook, stirring often, for 2 minutes.
If the sausage is in casings, remove it and crumble it into the skillet. Cook the sausage until browned, about 5 minutes.
Add the garlic and cook, stirring, for 30 seconds.
Add the marinara sauce, Italian seasoning, dried basil, black pepper, and red pepper flakes (if using). Stir and bring the sauce to a simmer. Reduce the heat to low and cook for 10-15 minutes.
Remove the skillet from the heat and spoon 1 cup of sauce into the bottom of a 13x9-inch baking dish, spreading it evenly. Gently stir ⅔ of the remaining sauce into the pasta. Transfer half of the saucy pasta to the baking dish and spread it into an even layer.
Dollop spoonfuls of ricotta cheese over the pasta in the baking dish. Sprinkle ⅓ of the shredded mozzarella and ¼ cup Parmesan cheese on top.
Spread the remaining pasta in an even layer over the cheese. Top with the remaining sauce, mozzarella, and Parmesan.
Spray one side of a sheet of foil with cooking spray and cover the baking dish, cooking spray side down to prevent the cheese from sticking.
Bake, covered, for 35 minutes. Uncover and bake for an additional 10-15 minutes, until the casserole is hot and bubbly and the cheese is golden brown. Let it rest for 10 minutes before serving.