A busy website that has new content being published regularly can quickly accumulate 404 errors due to changes of page URL's, tag or category name changes or deletions and so on.
Luckily, even if the count of missing pages has reached thousands or more, we can use some simple tools and 301 Redirect tricks to clean them up accordingly.
WP Redirection Plugin
There are a handful of WordPress 301 Redirect plugins available that offer features to aid in fixing 404 errors and URL changes on your website. The best plugin for full control over your URL Rewrites, however, is the Redirection Plugin.
The plugin allows most Regex (Regular Expression) commands when creating 301 redirect rules, as if you were adding them directly to your .htaccess file. You can view and download the Redirection plugin here.
Upon installing the plugin, we can begin with some typical 301 redirection scenarios.
WordPress 301 Redirecting
As you can see in the image below, there are a few basic input fields we will be using to redirect a URL, Source URL and Target URL. We don't need to check the Regular expression check-box unless we include a Regex in those fields.
Basic URL Rewrite
This is a basic URL rewrite example, without using Regex:
Source URL: http://website1.com/old-page.html
Target URL: http://website2.com/new-page/
http://website1.com/old-page.html => http://website2.com/new-page/
The above redirects the source URL to a unique target URL, ideal for fixing a unique 404 error. For reoccurring, similar 404 URL patterns, we look at some Regex URL Redirection examples:
Basic Regex URL Rewrite
If you're new to Regular Expression and if the below WP redirection examples seem confusing, then you may want to refer to the tutorial from Redirection to get you started.
Now, let's assume a scenario in which you first included your category slug in post permalinks and now want that changed to (remember to check the Regular expression checkbox):
1. No Category Slug in Permalink
Example Desired URL Sequence
http://website.com/category/post-title.html => http://website.com/post-title.html
Above URL Rewrite Using Regex
/category/(.*).html => /$1.html
2. A Changed Category Slug Rewrite
/category-old/(.*).html => /category-new/$1.html
3. Sub-Category Tree without Parent
If the permalink looked like /parent/child/post-name.html
and you only want /child/post-name.html
to display, the following rewrite can be used:
/parent/(.*)/(.*).html => /$1/$2.html
Intermediate Regex Rules
Here are some more rules that may be a bit more tedious than the previous ones, for the following WordPress URL rewrites:
1. Stripping File Extension from Permalink
Removing .html
/(.*).html => /$1/
Removing .php
In the case of .php
, you do not want to strip them off the WordPress core files, so we use the below rule to avoid that.
/((?!.*(sitemap|login|wp|xml)).*).php => /$1/
2. Redirect Only Date-Based URL
We can use (\d*)
to match only numbers, (\s*)
for letters or (.*)
for any character. So, if you want to rewrite a permalink like /2014/02/post-title/
and avoid matching category tree permalinks, this would resolve that:
/(\d*)/(\d*)/(.*) => /$3
# OR
/\d*/\d*/(.*) => /$1
WordPress URL Rewrite Examples
Hopefully, the above examples using the WP Redirection plugin were helpful in resolving most 301 redirect fixes. If any of the above is unclear, wrong or you would like to see more examples, please feel free to comment as usual.