https://docs.magento.com/m2/ce/user_guide/catalog/catalog-urls.html

Catalog URLs

The URLs you assign to products and categories play a major role in determining how well your site is indexed by search engines. Before you start building your catalog is an ideal time to consider the available options.

To configure catalog URLs:

1. On the Admin sidebar, tap Stores. Then under Settings, choose Configuration.
2. In the panel on the left under Catalog, choose Catalog.
3. Expand  the Search Engine Optimizations section, and do the following:
a. Set Product URL Suffix to “html” or “htm.” Enter the suffix without a period, because it is applied automatically.
b. Set Category URL Suffix to “html” or “htm.” Enter the suffix without a period, because it is applied automatically.
c. Set Use Categories Path for Product URLs to your preference.
4. When complete, tap  Save Config .
5. When prompted, click the Cache Management link in the system message, and refresh the invalid cache.