Changing Bitnami WordPress permalink structure without causing 404s and maintaining SEO

To streamline URIs because thats the cool thing to do.

I’ve been recently tinkering with the idea of using Ghost, another content management system (CMS), to manage the blog posts and back end stuff of my blog here since I find Ghost can be rather more streamlined and less.. “bloated” than WordPress can be at times. Having tinkered with Ghost in a container in Azure Container Instances, my initial plan was put on pause as I found that the overall cost to running a container in the cloud was just too cost prohibitive (almost 10x what I currently pay) at this time, more on that in another blog post I think.

What I liked about my brief test of Ghost was that not only does it have great features comparable to WordPress, but out of the box the post format was something like:

I checked my blog to see the format as it’s not something I really think about. Low and behold I’m using a similar format that’s not quiet WordPress out of the box , but is rather close to this new target state idea that I’d like to resemble Ghost. For reference, WordPress out of the box has a format that resembles:

That’s certainly not very Search Engine Optimisation (SEO) friendly. So very early on I changed that formatting to be 1) more SEO friendly and more importantly 2) user friendly. At the time I had a theme with specific archive pages for year, month and day of posts. With that I had the grandiose plan of writing very often and I thought that certainly readers would appreciate that level of searchability.

Fast forward to several theme revisions later and that level of search is now defunct. So I wondered if it would be easy enough to change the permalinks formatting now on the blog without breaking any links and SEO. I don’t expect much from SEO and while it certainly helps to get exposure, I didn’t want to just lose any of the momentum that the blog has gotten over the last 4 years.

My blog is hosted on AWS Lightsail and runs Bitnami WordPress instead of the official WordPress deployment. Bitnami WordPress is a customised instance of WordPress where Bitnami add in a number of improvements to the out of the box WordPress experience. I’m all for improvements. I do find that the Bitnami documentation is pretty comprehensive and I appreciate the effort in that doco. My problem is that there isn’t much specific detail on 301 redirects and there’s also a number of Bitnami WordPress images with slight tweaks for different cloud providers. With other bloggers out there documenting their own experiences, mine was not that straight forward; hence this blog post.

So thinking this through, the solution should go something like the following:

  • Find a means to do 301 redirects for each post from the old permalink formatting to the target state
  • Change the WordPress permalink formatting
  • Not lose and SEO and continue reaching lots of like minded people
  • Job done!

Hold on there slick.

As I said before, this was harder than I thought. Not impossible by any stretch of the imagination. It just needed some thorough research as the overwhelming majority of useful information just referenced the most common recommended approach of putting in redirects in the Apache web server .htaccess file. That’s just a configuration file that allows 301 redirects to happen (among a bunch of other good stuff) at the server level.

Now I could look to do this in Cloudflare as I use their excellent service (love their work! Before you ask: no, this isn’t a paid endorsement). So rather than the request ever hitting my server, the redirect could happen at the edge. Thus reducing my cloud cost. More edge = less cloud cost. My wallet and I both love that. I didn’t want to explore that option as I think the .htacess approach is faster.

After spending quite a bit of time reading not only Bitnami knowledge base articles, to WordPress knowledge base articles, to blog posts to even YouTube videos on the topic, I managed to collate a few sources together into a final solution. What was the key final find was on the yoast.com website which provide SEO plugins and services for WordPress. I use the plugin, very, very lightly and really if I lost it tomorrow, it wouldn’t make much of an impact to the blog. So with that, I didn’t want to fork out for a subscription for the service.

Digging deeper and I eventually stumbled upon a blog post that Yoast put out there about this requirement. When searching around that topic I then got to a helper service that Yoast has freely on their website for the exact redirect I wanted to do! Progress! Bellow I go into how to use that helper, so I’ll leave that there. Now, the only other thing to get this working was this .htaccess file.

Bitnami WordPress moves the functionality of the .htacess file to a new folder in its file structure and into a file altogether called htaccess.conf. This was part two of the harder than I thought problem solving here. There’s a bunch of mixed information out there that references this file (the htacess.conf). Additionally there’s a few another key files that come up quite often in Google, like httpd.conf, httpd.app.conf and httpd-vhosts.conf. I’m not that experienced with Bitnami Wordress or Apache to know if these other config file amending solutions are correct or not. They may well offer the same solution.

After playing around early one morning over the last week and effectively breaking traffic getting to to the blog, I eventually got the solution below using the htacess.conf file. I just wanted to outline that as surely there’s more than one way to skin a cat. This is the way it worked and continues to work for me.

The solution walk through

  • Log onto to your Bitnami WordPress with the SSH client of your choosing
  • Enter in the following to bring up the htaccess.conf file:

cd opt/bitnami/apps/WordPress/conf sudo nano htaccess.conf

  • Select the top line of the config file
  • Press enter a couple of times to give yourself some space
  • Via your favourite browser, go to https://yoast.com/research/permalink-helper.php
  • You’ll find a couple of key fields, as per the screenshot bellow:

  • Enter in the particulars relevant to your site, namely:
    • “URL of your site (start with http:// or https://) :”
    • Select the web server you’re running in “My web server is running on” (either Apache or NGINX)
    • No select “Your old permalink structure:” (mine was previously Month and Name"
    • Lastly select “Generate Redirect”
  • This will generate the following when the page refreshes:

  • Copy this output
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(?!page/)(.+)$ https://lucian.franghiu.com/$3
  • From what I understand, this output will be a permanent redirect for any blog posts in the previous format to the current structure
    • It grabs the year ([0-9]{4}) that is 4 characters
    • It grabs the month ([0-9]{2}) that is 2characters
    • Then whatever the URL is after that (?!page/)(.+)$
    • Redirects to a URI of https://lucian.franghiu.com/$3 with $3 being the new permalink format of just the domain/post
  • Paste this into htaccess.conf at the very top line of the file
    • Here’s what my file looks like for reference:

  • Now press Ctrl + X (to exit and save)
  • Press Y (to save)
  • And finally press enter (to confirm file name)
  • Now to make sure this is all good, restart Apache web server by entering:

sudo /opt/bitnami/ctlscript.sh restart apache

  • Lastly, change the permalink format in WordPress
    • Go to WordPress admin console
    • Select Settings > Permalinks
    • Change the permalink format from Month + Name to Name only
  • All done

Enjoy!

Read more on:
Comments...
Disclaimer
Shout out 👍