Skip to main content

Greg McVerry

Migrating WordPress Blog Step Two: Redirects

4 min read

As 2018 draws to a close I am saying goodbye to WordPress. In order to accomplish this I decided to archive my blog rather than worry about importing eleven years worth of content.

Previously in this series I described how to set up a subdomain and add a redirect plugin for internal links on your WordPress site.

But what about the links on the web? How do you get https://example.com to point to https://archive.example.com?

What is a redirect?

A redirect tells a browser, "even though the person entered this url go to this website instead."

Have you tried to go to a website and ever found a page with a number error like 404 or 300 something? Those 300 errors, specifically 301 and 302 indicate a page redirect.

  • 300 is any general redirect
  • 301 indicates a page has permanently moved
  • 302 indicates a temporary redirect that browsers should ignore in future

I needed to set up a 301 page. I asked in the chat about how to proceed and learned about .htaccess files.

Editing .htaccess

As a shared host customer I needed to determine what kind of server my site lived on ngix or Apache. Having edited my .htaccess for WordPress in the past I knew I had an Apache server.

If you use a shared host you can access your .htaccess file through your file manager or through sftp. You may have to turn on "hidden" files.

In c-panel, for example, to find .htaccess, go to cPanel>File Manager> website directory of your choice. Go to the top, right-hand corner of the screen and click Settings.

So on my new website I added a file and named it .htacess I then added this code to point any url request to https://jgregorymcverry.com that returned a missing directory or file to https://archive.jgregorymcverry.com

RewriteEngine On
RewriteCond %{HTTP_HOST} ^jgregorymcverry.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://archive.jgregorymcverry.com/$1 [R=301,L]

How Does it Work?

The "RewriteEngine On" activates a set of tools on Apache servers to help point URL to different places on the web.

In the next line I set a condition for rewriting the url. I tell the server with the ^ to start looking at jgregorymcverry.com. The NC means "no case" or basically do not be case sensitive and accept upper or lowercase.

In the next condition the -file and the ! basically is a negative. I am saying rewrite the url if you find a url that points to a missing file (which would be any link to a page on my old blog).

The next condition does the same for directories as it does for files. In a url the directory is the stuff in dashes like https://archive.jgregorymcverry.com/October/post.html. October is a folder or directory containing files.

In the next line I add the rewrite rule. The $ sign ends the rule that the carrot began. We call this a string. The (.*) says accept any file or directory extension. The * means match anything. The $1 then says add whatever was found to my new url https://archive.jgregorymcverry.com.

The 301 then tells other websites like search engine that this change is permanent.

What is Regex?

The htaccess file gets written in regular expression. This symbolic language is as old as the field of computer science. Learning about regex and how to write redirects is not easy.

The official documentation is dense and the tutorials on the web got polluted with SEO and WordPress advice. In fact I would add -seo -wordpress to any search about "301 redirects" or "htaccess and redirect."

This tutorial only works for my specific use case. Though it is the most generic redirect one could write.

In the end I could not solve my problem without help. Before begining the web I posted my  redirect to IndieWeb chat and that put me on to the htaccess file. I then I posted my attempt and Colin Walker helped me and ended up writing the redirect rules.

This demonstrates how important networks are for learning. The text around writing the redirects were useless for someone trying to learn. 

I hope this tutorial helps others archiving and migrating a WordPress website. If you are on a shared host you may just want to file a trouble ticket as your company will probably take care of this for you.