I’ve recently switched to using nginx + php-fpm + mariaDB for the backend running this WordPress blog 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 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.
With my new install I wanted to make sure I have my SEO sorted. I did some research on the best permalink structure and found a post by the ever popular Yoast that mentions that you should just use post name and nothing else.
This meant I had to move all my existing content over to the new structure which left me with index links like the following:
https://digitalchild.info/134/apple-magic-mouse-keeps-disconnecting.html
and I needed to change them to
https://digitalchild.info/apple-magic-mouse-keeps-disconnecting/
As I am new to Nginx I went to the trusty google and after several hours of hacking and slashing I found an answer. Now I’ve read that if statements are evil in Nginx 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.
Here is the nginx rewrite rule for html files I used to rewrite my old URL to the new one.
# This directive is due to legacy urls for SEO. location ~ \.html$ { # Stop if the url corresponds to a real .html file if (-f $request_filename) { expires 30d; break; } #if the file doesn't exists remove the .html extension and continue if (!-e $request_filename) { rewrite ^(/.+)\.html$ $scheme://$host$1 permanent; } }