Preheat your oven to 375°F (190°C).
Cook the mostaccioli pasta according to package instructions until al dente. Drain and set aside.
In a large skillet, heat the olive oil over medium heat. Add the chopped onion and garlic, sautéing until the onion is translucent, about 3-4 minutes.
Add the ground beef or sausage to the skillet. Cook until browned, breaking it apart with a spatula, about 5-7 minutes. Drain excess fat if necessary.
Stir in the marinara sauce, Italian seasoning, red pepper flakes, salt, and pepper. Let simmer for about 5 minutes.
In a large bowl, combine the cooked mostaccioli, meat sauce mixture, and ricotta cheese. Mix until well combined.
Transfer half of the pasta mixture to a greased 9x13-inch baking dish. Sprinkle half of the mozzarella and Parmesan cheese on top. Layer the remaining pasta mixture and top with the remaining cheeses.
Cover the baking dish with aluminum foil and bake for 20 minutes. Remove the foil and bake for an additional 10-15 minutes, or until the cheese is bubbly and golden.
Let cool for 5 minutes before serving. Garnish with fresh basil or parsley if desired.