301 redirects not working with WordPress (the solution)

I just moved my site over from github pages to WordPress. In doing so, my post URLs changed, and I wanted to redirect so that visitors from search results wouldn’t hit a ‘page not found’.

My webhost uses cPanel, so I followed the instructions to set up 301 redirects for my pages. But they didn’t work! A bit of googling turned up that redirects don’t always work as expected when using content management systems like WordPress, but didn’t provide the answer. So here it is!

Because WordPress rewrites URLs so that it can handle pretty permalinks, your redirect (rewrite) rules must go above the WordPress section in your .htaccess file:

RewriteCond %{HTTP_HOST} ^heycascadia\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.heycascadia\.com$
RewriteRule ^2014\/08\/08\/arduino\-wireless\-freezer\-alarm\/$ "http\:\/\/www\.heycascadia\.com\/arduino\-wireless\-freezer\-alarm\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^heycascadia\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.heycascadia\.com$
RewriteRule ^2014\/08\/19\/blackberry\-workgloves\/$ "http\:\/\/www\.heycascadia\.com\/blackberry\-proof\-workgloves\/" [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

If they’re below the # BEGIN WordPress section, the URLs will be rewritten first, and then won’t match your redirect rules.

Leave a Reply

Your email address will not be published. Required fields are marked *