{"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 <\/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\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