Redirect WordPress Date Archives with .htaccess
Restructuring a WordPress website may involve removing the subdomain from URLs/permalinks. For example, I recently removed the original WP-install subdirectory from Perishable Press to simplify site structure and optimize WordPress permalinks. There are PHP scripts and WP plugins that might work for this, but in most cases .htaccess is optimal when changing URL structure and redirecting traffic. Here’s a quick example to help visualize the concept:
Old structure (w/ subdomain)
New structure (w/out subdomain)
We also want to redirect date-archive permalinks when they’re paged or missing the trailing slash. Note that here we’re dealing with WordPress date archives, which is different than simply removing the “year/month/day” from your single-post permalinks. We’ll get to doing that a bit later, for now let’s remove the subdirectory from the date-based archives.
Redirecting date archives
First, if you’re familiar with using .htaccess, you may be asking, “why is this even necessary? Why not just redirect *everything* from the subdirectory?” Good question. If you have WP installed in a subdirectory structure like this:
..and want to redirect to basically the same thing, only without the
such that there is basically a one-to-one correspondence between old and new files, then YES you can just redirect everything wholesale with a simple line of .htaccess:
RedirectMatch 301 ^/wordpress/(.*) http://example.com/$1
and be done with it — no need to bother with the other redirect methods in this tutorial. If, however, your site restructuring is more complicated, perhaps involving stuff like:
- you also want to remove the year/month/day from your single-post permalinks
- you are removing/renaming/consolidating directories, so not 1-to-1
- you are changing tag/category/taxonomy names, so not 1-to-1
- other subdirectory installations of WordPress are involved
- you’re just nuts for even thinking about this crazy stuff
If any of these apply, then the easy way is out and you’ve got some thinking to do. I’m here to help with that, starting with redirecting your date-based archives. Here is the magic code to add to your site’s root .htaccess file (after making a backup copy of the original):
# REDIRECT WP DATE ARCHIVES RedirectMatch 301 ^/wordpress/([0-9]+)/?$ http://example.com/$1/ RedirectMatch 301 ^/wordpress/([0-9]+)/page/(.*)$ http://example.com/$1/page/$2 RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/?$ http://example.com/$1/$2/ RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/page/(.*)$ http://example.com/$1/$2/page/$3 RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/([0-9]+)/?$ http://example.com/$1/$2/$3/ RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/([0-9]+)/page/(.*)$ http://example.com/$1/$2/$3/page/$4
There are three sections to this code: yearly archives, monthly archives, and daily archives. The only editing that you need to do is change the “
wordpress” in each line to match your subdirectory’s name, and then also replace all instances of
example.com with your domain name.
Eliminate all date-based archives
You may have asked at some point, “why bother at all with date-based archives?” After all, it’s redundant info that may contribute to “duplicate-content” penalties in search-engine results. I think most blogs these days emphasize categories and tags over the myriad other archive types. Many themes tend to ignore the date-based archives and popular SEO plugins provide easy ways of no-indexing/removing them from search engines. So let’s just eliminate date archives entirely, say by redirecting all of it to your home page, where the additional link equity may provide some SEO benefits to your site. So that’s the spiel — here’s how to do it..
Add the following code to your site’s root .htaccess file:
# REDIRECT DATE ARCHIVES TO HOME PAGE RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)?/?([0-9]+)?/?$ http://example.com/ RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)?/?([0-9]+)?/?page http://example.com/
As discussed in the previous section, edit or remove the subdomain to match your own, and then change the
example.com to whatever makes sense (e.g., your home page). Test thoroughly using as many different archive/permalink formats as possible.
Once the date archives have been redirected via .htaccess, you should go through your theme and remove/edit any date-based archive links and/or code. A typical location for such would be
index.php, and of course
date.php if it actually exists. Once your theme is cleaned up, you’re all set, but should keep an eye on any weird activity in your traffic logs. As straightforward as it seems, it’s a big move, and you want to be careful.
Removing year/month/day from single-post permalinks
Removing the year/month/day from your single-post permalinks is another way to improve the SEO integrity of your WordPress-powered site. Here’s the code to do it via .htaccess:
# REMOVE DATE FROM WP POST PERMALINKS RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://example.com/$4
As-is, this technique also removes the subdirectory (
/wordpress/), so edit accordingly or remove if not needed. Once in place, this code will change your single-view permalinks from overkill:
Remember to test thoroughly after making any changes! As always, questions & comments welcome. Thanks for reading :)
often redirect can be bad for google Indexed.
That’s pretty vague.. care to elaborate a bit?
Sorry, i means often modify the url structure, may be the search engine will be unfriendly.
Ah yes, so true – they can be unfriendly even when you don’t modify anything ;)
Could this technique be adapted to do things like removing the category base from the url? Something like:
RedirectMatch 301 ^/category/(.*)$ http://example.com/category/$1
In theory yes it should work the same way.. the question is how will it affect WP functionality. I’ve played with trying to remove category and category-parent slugs from the URL but no success.
Finally got the code I’m looking for. Thanks Jeff.
Thank ya kindly homeslice. The redirects work perfectly!
As this post, I have a question, long long time ago, i have a link structure look as:
Now,i want to remove the
how can i use your method to remove it
it’s important to me, hope your help，i’ll be very grateful
If I understand correctly, something like this should work:
RedirectMatch 301 /blog/web-design-blog/ http://example.com/blog/
More tuning will probably be needed, but that’s the basic idea.
Editors note: Just finished switching servers and had to recreate some comments that were posted on the old server. Just in case you were wondering.
RedirectMatch 301 ^/blog/web-design-blog/(.*)$ http://example.com/blog/$1
the problem is OK, thanks again~
Thanks for the post. I installed my blog in the wordpress folder so, at present, I have a redirect for those who type example.com to be redirected to example/wordpress.com
How can I keep that redirect and institute this for posts
RedirectMatch 301 ^/wordpress/(.*) http://example.com/$1
Thanks Jeff, Finally the code I needed….
Great tutorial Jeff. Just to be sure. I am working on a blog right now and I want to redirect the permalinks from:
Does the correct redirect look like this?
# REMOVE DATE FROM WP POST PERMALINKS
RedirectMatch 301 ^([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://example.com/category/$4
I am sure it does not because we the date does not correspond with the individual categories, does it? Is there even a way to solve that issue with .htaccess ?
Correct, in order for the category to be used in the target URI, it must be available in the initial request. I would set up a test install of WP, create your original (old) permalinks, then change them to include/use the category. The point is, WordPress should automatically take care of the redirects for new and existing content. It’s only for external/non-WP links that will need to be manually redirected. In this case, .htaccess may not suffice, but PHP should get you there.
This is what i was looking for long time.Thanks for the code and i will fix permalink related issues.
Hi, I have some problems with custom post types (WP) URL. I cannot find a solution.
The problem is as follows:
My theme has generated automatic URL’S for each custom post type.
I can not bring back from these addresses to the original article. If I create a folder and add there a htaccess works but there is no direction for search engines or social networks.
How can I fix this? Is there a way?
Thanks! Exactly what I needed to do some redirect magic. As a general blog, we still prefer to keep our date based archives for easy reference.