When attempting to migrate, say, an old WordPress blog to another platform, such as Ghost, you may find yourself worried about losing preexisting links to your content on your old site. While this can also affect search engine optimization, perhaps the most irritating side effect of this occurrence is your audience reaching 404 errors whenever they attempt to follow bookmarks to your site.
While redirecting the whole domain is simple enough— there is a Redirects option in cPanel that makes the process quite simple— there could still be folks who have links to your RSS feed in their feed readers who likely wouldn't get the update and be quite lost.
Fortunately, we can address this by writing a few lines in an .htaccess file to dynamically redirect to specific locations based on where a person entered the old site.
What is .htaccess?
.htaccess is a configuration file that is used on all servers running Apache. The .htaccess file allows you to enable/disable Apache functions and capabilities, so it’s essentially your personal overriding feature.
An .htaccess file is loaded, detected, and executed by the Apache Web server software. The .htaccess file sometimes comes with a few lines by default. For example, the WordPress .htaccess file will redirect a URL like labrumfield.com/index.php to labrumfield.com because there’s a rule that will tell the server that index.php is what the server should display.
We can write commands of our own to redirect certain URLs as well. Let's take the following site as an example: http://archive.timmmmyboy.com.
All of the posts have the following structure:
http://archive.timmmmyboy.com/2013/11/writing-collaborative-documentation-with-dokuwiki-and-github/
So our URL has the base domain, followed by the year, followed by the month, followed by the name of the post.
Now let's say we want to redirect to a new Ghost blog has this format for posts:
http://blog.timowens.io/writing-collaborative-documentation-with-dokuwiki-and-github/
This URL doesn't have the year or the month included, but we can grab the last part of the WordPress URL and use that to redirect to the new Ghost URL.
Adding the Redirect
What we want to do is have Apache grab the post name (writing-collaborative-documentation-with-dokuwiki-and-github) from the end of the WordPress URL and append it to the domain of the new blog in Ghost.
Here’s what the .htaccess file would look like once we add the redirects:
RewriteEngine On
RewriteRule ^([0-9]+)/([0-9]+)(.)$ http://blog.timowens.io$3 [R=301,L]
RewriteRule ^feed$ http://blog.timowens.io/rss [R=301,L]
RewriteRule ^feed/$ http://blog.timowens.io/rss [R=301,L]
RewriteRule ^(.)$ http://blog.timowens.io/ [R=301,L]
Let’s go line by line and see what it’s doing.
Adding the Rewrite Rule
RewriteEngine On
We need to tell Apache we’re going to be rewriting some URLs so this has to come before any RewriteRule directives.
RewriteRule ^([0-9]+)/([0-9]+)(.*)$ http://blog.timowens.io$3 [R=301,L]
- RewriteRule is the directive to create the rewrite.
- The ^ is a wildcard saying “ignore whatever came before the pattern I’m about to show”.
- ([0-9]+)/([0-9]+) are regular expressions that mean "a series of numbers between 0 and 9 will show up between these slashes).
- (.*)$ is our final wildcard “anything coming after that pattern” and the $ tells the server to store each of the wildcard directives of that line to a variable.
301 vs 302 Redirects
The second part of that rule allows us to grab that variable (in this case the third variable, the one that holds our post name) and append it to the end of our new URL. [R=301,L] tells the server this is a 301 Permanent Redirect which the server will then let Google and other entities know when they visit.
It’s highly highly recommended that you use 302 Temporary Redirects until you’re confident you’ve got it right. Temporary redirects will not store a cookie in your browser causing the redirect to be cached, nor will search engines be notified while you’re testing.
RSS Feed Redirects
The link to my RSS feed is different and is essentially saying that if someone visits a link ending in the word "feed," then redirect it here.
- RewriteRule ^feed$ http://blog.timowens.io/rss [R=301,L]
- RewriteRule ^feed/$ http://blog.timowens.io/rss [R=301,L]
There are two lines for "feed" and "feed/" because some people may include the trailing slash when typing in the URL.
"Catchall" Redirect
Finally we'll a catchall redirect:
- RewriteRule ^(.*)$ http://blog.timowens.io/ [R=301,L]
This one is for anyone entering a URL without a post or directive on the end, and it will redirect them to the new homepage.
Once you’ve tested it and know it’s working switch from 302 Temporary Redirects to 301 Permanent Redirects and you’re all set!
Notes
Quick note about .htaccess files: Because of the dot on the beginning of the filename some programs may not show the file by default since that’s universal for “hidden files”.
When working in cPanel > File Manager you can check an option in the Settings to show hidden files: