Having had an infection doesn't always mean you're protected from then on.
Immunity after infection can wane
For many diseases, immunity after infection falls over time. Flu viruses change every year, and the COVID-19 virus keeps producing new variants, so reinfection is possible even after you've been ill. Regular vaccination helps maintain protection, especially against the strains circulating now.
Some diseases don't produce immunity after infection
Tetanus is the classic example: having had tetanus doesn't make you immune to it, so the recommended vaccination and boosters are still needed.
Vaccines can give broader coverage
HPV has many types. Even if you've had one, the vaccine can still protect against other high-risk types.
When to vaccinate after infection
You can usually be vaccinated after recovery, with the exact timing depending on the disease. After COVID-19, for example, a gap before the next dose is generally advised. If you're unsure about your situation, confirm the timing and need with a doctor.