One of the things I highly value is stable URLs. When you link to my site, I want that URL to work as long as my site is up. I don't want to lose all those links lightly. But for ease of implementation, the URL structure matches the file structure on my site, and I occasionally want to change the file structure. So what should I do?

I make a 301 redirect from the old URL to the new URL.

There are links on forums, discord, stackoverflow, etc. that I can't expect anyone to change. But I can keep those links working on my end by adding a redirect. I have made 28 of these URL changes in 28 years, an average of once year, and each one has a redirect to keep the old URLs working.

In this case I wanted to move https://www.redblobgames.com/grids/line-drawing.html to https://www.redblobgames.com/grids/line-drawing/ . The original plan back in 2011 was to have a main page about grids and then a few pages that went along with that. The line drawing page was one one the accompanying pages. But I never did write a main page, so now the line drawing page is its own project. I want to move it into its own folder so that I can add more pages to the project, including a comparison to Bresenham's Line Algorithm. Here were the steps:

  1. Move the files
cd grids/
mkdir line-drawing
mv line-drawing.bxml line-drawing/index.bxml
mv line-drawing.html line-drawing/index.html
mv line-drawing.js line-drawing/line-drawing.js
mv line-drawing.org line-drawing/index.org
mv line-drawing.png line-drawing/
mv _line-drawing-helper.js line-drawing/
  1. Test the new page locally
  2. I run a staging server locally so that I can test all of my pages before I upload them. I want to make sure I get all the supporting files, and I also want to make sure any relative links to ../ get changed to ../../. I use the Network panel in the browser tools to make sure there are no 404s. It turns out I had missed some files, so I'm glad I tested this before uploading it.

  3. Create a permanent redirect
  4. I'm using Nginx for redblobgames.com, so I added this to nginx.conf:

    location = /grids/line-drawing.html {
      return 301 $scheme://www.redblobgames.com/grids/line-drawing/;
    }
    

    I then have to tell nginx to load the updated configuration file:

    sudo service nginx reload
    
  5. Test the redirect
  6. It turns out I had gotten this wrong, initially putting location ~. I rarely edit my nginx file so I keep forgetting the various rules for matchers.

    This means the site was broken for a few minutes while I fixed the bug. If I were more disciplined I'd have a staging server for testing nginx configuration, but my staging server runs Caddy so I don't catch this type of bug. I rarely change my nginx.conf though, so it's a low priority to add tests here.

  7. Update links from the rest of my site
  8. Since I set up a redirect, I don't have to update the links, but I think it's good to do it anyway. My upload script checks local links and gave me a list of pages to update:

    I went through each one and changed the links manually. I don't have an automated tool for this (other than occasionally using perl -i -pe) because it's not a common thing I do.

    However I still have links on my blog that aren't included here. Moving my blog from blogspot to my own site is another project. Until then, I have search for links in two places.

    I went through each one and changed the links manually. I can't use perl -i -pe here because blogspot requires me to edit through their editor instead of editing files.

I think everything works now.

Also see: Cool URIs don't change

Labels:

1 comment:

Reader wrote at September 17, 2023 8:21 AM

Thanks for keeping it updated!