{"id":15893,"date":"2019-10-22T11:38:01","date_gmt":"2019-10-22T11:38:01","guid":{"rendered":"https:\/\/blog.taragana.com\/?p=15893"},"modified":"2019-11-24T04:43:26","modified_gmt":"2019-11-24T04:43:26","slug":"static-page-sites-how-to-clean-url-structure-on-nginx","status":"publish","type":"post","link":"https:\/\/blog.taragana.com\/static-page-sites-how-to-clean-url-structure-on-nginx-15893","title":{"rendered":"Static Page Sites: How to clean URL structure on Nginx"},"content":{"rendered":"\n
Static Sites are all the rage these days because they are:<\/p>\n\n\n\n
However, you will often find broken links due to minor changes in the URL structure, which a CMS like WordPress will intelligently handle. To solve this, I propose a simple structure for all static pages:<\/p>\n\n\n\n
\/a\/<\/code> OR \/a\/index.html<\/code> OR \/a.html<\/code> all redirects to \/a<\/code> which is served by \/a.html<\/code><\/li>\/a\/b.html<\/code> OR \/a\/b\/<\/code> or \/a\/b\/index.html<\/code> all redirects to \/a\/b<\/code> which is served by \/a\/b.html<\/code><\/li><\/ul>\n\n\n\nThis creates a unified view and without inconsistencies while resolving common error cases. The corresponding Nginx configuration snippet is:<\/p>\n\n\n\n
server {\n listen 80 default_server;\n listen [::]:80 default_server;\n\n rewrite ^\/index\/?$ \/ permanent;\n rewrite ^\/$ \/index.html break;\n rewrite ^(.+)\/+$ $1 permanent;\n rewrite ^(.+)\\.html $1 permanent;\n rewrite ^(.+)\/index(\\.html)?$ $1 permanent;\n\n root \/var\/www\/html;\n index index.html index.htm;\n server_name _;\n location \/ {\n try_files $uri $uri.html $uri\/ =404;\n }<\/code><\/pre>\n\n\n\n