{"id":333,"date":"2014-06-18T11:31:18","date_gmt":"2014-06-18T11:31:18","guid":{"rendered":"https:\/\/digitalchild.info\/?p=333"},"modified":"2014-06-18T11:31:18","modified_gmt":"2014-06-18T11:31:18","slug":"nginx-rewrite-rule-html-files","status":"publish","type":"post","link":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/","title":{"rendered":"Nginx Rewrite Rule for Html Files"},"content":{"rendered":"

I’ve recently switched to using nginx<\/a> + php-fpm<\/a>\u00a0+ mariaDB<\/a>\u00a0for the backend running this WordPress<\/a>\u00a0blog and running it on a micro instance on amazon web services<\/a>. I did this because I wanted to learn some newer technologies and see how they differ from the standard LAMP setup that I’m so familiar with. While doing so I for some foolish reason had my WordPress permalink structure set so that all my urls ended with a .html. I did this 5 years ago and cannot for the life of me remember why. Now that I’ve moved over and I have a few popular posts that are still linking to these old urls I needed a way to have people find them without having to search the site.<\/p>\n

With my new install I wanted to make sure I have my SEO sorted. I did some research on the best permalink structure and\u00a0found a post by the ever popular Yoast<\/a> that mentions that you should just use post name and nothing else.<\/p>\n

This meant I had to move all my existing content over to the new structure which left me with index links like the following:<\/p>\n

https:\/\/digitalchild.info\/134\/apple-magic-mouse-keeps-disconnecting.html<\/a><\/p>\n

and I needed to change them to<\/p>\n

https:\/\/digitalchild.info\/apple-magic-mouse-keeps-disconnecting<\/a>\/<\/p>\n

As I am new to Nginx I went to the trusty google and after several hours of hacking and slashing I found an answer<\/a>. Now I’ve read that if statements are evil in Nginx<\/a> but I can’t get any of the try_files statements I configured to work. If you know a better way to do this please let me know in the comments.<\/p>\n

Here is the nginx rewrite rule for html files I used to rewrite my old URL to the new one.<\/p>\n

\n\n# This directive is due to legacy urls for SEO.\nlocation ~ \\.html$ {\n# Stop if the url corresponds to a real .html file\nif (-f $request_filename) {\nexpires 30d;\nbreak;\n}\n\n#if the file doesn't exists remove the .html extension and continue\nif (!-e $request_filename) {\nrewrite ^(\/.+)\\.html$ $scheme:\/\/$host$1 permanent;\n}\n}\n\n<\/pre>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

I’ve recently switched to using nginx + php-fpm\u00a0+ mariaDB\u00a0for the backend running this WordPress\u00a0blog and running it on a micro instance on amazon web services. I did this because I wanted to learn some newer technologies and see how they differ from the standard LAMP setup that I’m so familiar …<\/p>\n

Nginx Rewrite Rule for Html Files<\/span> Read More \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[5],"tags":[113,132,165,168],"yoast_head":"\nNginx Rewrite Rule for Html Files - Random Adult<\/title>\n<meta name=\"description\" content=\"I needed an nginx rewrite rule for html files and this is the answer I found after several hours of searching and testing.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nginx Rewrite Rule for Html Files\" \/>\n<meta property=\"og:description\" content=\"I needed an nginx rewrite rule for html files and this is the answer I found after several hours of searching and testing.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/\" \/>\n<meta property=\"og:site_name\" content=\"Random Adult\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-18T11:31:18+00:00\" \/>\n<meta name=\"author\" content=\"digitalchild\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@dcwhatwhat\" \/>\n<meta name=\"twitter:site\" content=\"@dcwhatwhat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"digitalchild\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/\"},\"author\":{\"name\":\"digitalchild\",\"@id\":\"https:\/\/randomadult.local\/#\/schema\/person\/b0b954202434291ace51b4e7a0a81c04\"},\"headline\":\"Nginx Rewrite Rule for Html Files\",\"datePublished\":\"2014-06-18T11:31:18+00:00\",\"dateModified\":\"2014-06-18T11:31:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/\"},\"wordCount\":355,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/randomadult.local\/#organization\"},\"keywords\":[\"linux\",\"nginx\",\"rewrite\",\"rules\"],\"articleSection\":[\"Geek\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/\",\"url\":\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/\",\"name\":\"Nginx Rewrite Rule for Html Files - Random Adult\",\"isPartOf\":{\"@id\":\"https:\/\/randomadult.local\/#website\"},\"datePublished\":\"2014-06-18T11:31:18+00:00\",\"dateModified\":\"2014-06-18T11:31:18+00:00\",\"description\":\"I needed an nginx rewrite rule for html files and this is the answer I found after several hours of searching and testing.\",\"breadcrumb\":{\"@id\":\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/randomadult.local\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Nginx Rewrite Rule for Html Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/randomadult.local\/#website\",\"url\":\"https:\/\/randomadult.local\/\",\"name\":\"Random Adult\",\"description\":\"Ramblings of a random adult\",\"publisher\":{\"@id\":\"https:\/\/randomadult.local\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/randomadult.local\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/randomadult.local\/#organization\",\"name\":\"Random Adult\",\"url\":\"https:\/\/randomadult.local\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/randomadult.local\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/randomadult.local\/wp-content\/uploads\/2020\/01\/logo.png\",\"contentUrl\":\"https:\/\/randomadult.local\/wp-content\/uploads\/2020\/01\/logo.png\",\"width\":320,\"height\":134,\"caption\":\"Random Adult\"},\"image\":{\"@id\":\"https:\/\/randomadult.local\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/twitter.com\/dcwhatwhat\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/randomadult.local\/#\/schema\/person\/b0b954202434291ace51b4e7a0a81c04\",\"name\":\"digitalchild\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/randomadult.local\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e0331079f6730910bc7760cb8f781237?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e0331079f6730910bc7760cb8f781237?s=96&d=mm&r=g\",\"caption\":\"digitalchild\"},\"url\":\"https:\/\/randomadult.local\/author\/digitalchild\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Nginx Rewrite Rule for Html Files - Random Adult","description":"I needed an nginx rewrite rule for html files and this is the answer I found after several hours of searching and testing.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/","og_locale":"en_US","og_type":"article","og_title":"Nginx Rewrite Rule for Html Files","og_description":"I needed an nginx rewrite rule for html files and this is the answer I found after several hours of searching and testing.","og_url":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/","og_site_name":"Random Adult","article_published_time":"2014-06-18T11:31:18+00:00","author":"digitalchild","twitter_card":"summary_large_image","twitter_creator":"@dcwhatwhat","twitter_site":"@dcwhatwhat","twitter_misc":{"Written by":"digitalchild","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/#article","isPartOf":{"@id":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/"},"author":{"name":"digitalchild","@id":"https:\/\/randomadult.local\/#\/schema\/person\/b0b954202434291ace51b4e7a0a81c04"},"headline":"Nginx Rewrite Rule for Html Files","datePublished":"2014-06-18T11:31:18+00:00","dateModified":"2014-06-18T11:31:18+00:00","mainEntityOfPage":{"@id":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/"},"wordCount":355,"commentCount":0,"publisher":{"@id":"https:\/\/randomadult.local\/#organization"},"keywords":["linux","nginx","rewrite","rules"],"articleSection":["Geek"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/","url":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/","name":"Nginx Rewrite Rule for Html Files - Random Adult","isPartOf":{"@id":"https:\/\/randomadult.local\/#website"},"datePublished":"2014-06-18T11:31:18+00:00","dateModified":"2014-06-18T11:31:18+00:00","description":"I needed an nginx rewrite rule for html files and this is the answer I found after several hours of searching and testing.","breadcrumb":{"@id":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/randomadult.local\/nginx-rewrite-rule-html-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/randomadult.local\/"},{"@type":"ListItem","position":2,"name":"Nginx Rewrite Rule for Html Files"}]},{"@type":"WebSite","@id":"https:\/\/randomadult.local\/#website","url":"https:\/\/randomadult.local\/","name":"Random Adult","description":"Ramblings of a random adult","publisher":{"@id":"https:\/\/randomadult.local\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/randomadult.local\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/randomadult.local\/#organization","name":"Random Adult","url":"https:\/\/randomadult.local\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/randomadult.local\/#\/schema\/logo\/image\/","url":"https:\/\/randomadult.local\/wp-content\/uploads\/2020\/01\/logo.png","contentUrl":"https:\/\/randomadult.local\/wp-content\/uploads\/2020\/01\/logo.png","width":320,"height":134,"caption":"Random Adult"},"image":{"@id":"https:\/\/randomadult.local\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/twitter.com\/dcwhatwhat"]},{"@type":"Person","@id":"https:\/\/randomadult.local\/#\/schema\/person\/b0b954202434291ace51b4e7a0a81c04","name":"digitalchild","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/randomadult.local\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e0331079f6730910bc7760cb8f781237?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e0331079f6730910bc7760cb8f781237?s=96&d=mm&r=g","caption":"digitalchild"},"url":"https:\/\/randomadult.local\/author\/digitalchild\/"}]}},"_links":{"self":[{"href":"https:\/\/randomadult.local\/wp-json\/wp\/v2\/posts\/333"}],"collection":[{"href":"https:\/\/randomadult.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomadult.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomadult.local\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomadult.local\/wp-json\/wp\/v2\/comments?post=333"}],"version-history":[{"count":0,"href":"https:\/\/randomadult.local\/wp-json\/wp\/v2\/posts\/333\/revisions"}],"wp:attachment":[{"href":"https:\/\/randomadult.local\/wp-json\/wp\/v2\/media?parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomadult.local\/wp-json\/wp\/v2\/categories?post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomadult.local\/wp-json\/wp\/v2\/tags?post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}