Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

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)
https://perishablepress.com/press/2011/
https://perishablepress.com/press/2011/02/
https://perishablepress.com/press/2011/02/15/

New structure (w/out subdomain)
https://perishablepress.com/2011/
https://perishablepress.com/2011/02/
https://perishablepress.com/2011/02/15/

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:

example.com/wordpress/
example.com/wordpress/wp-admin/
example.com/wordpress/wp-content/
example.com/wordpress/wp-includes/
...

..and want to redirect to basically the same thing, only without the wordpress subdirectory:

example.com/
example.com/wp-admin/
example.com/wp-content/
example.com/wp-includes/
...

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..

Step 1

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.

Step 2

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 archive.php, archives.php, 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:

http://example.com/wordpress/2012/04/12/post-name/

..to elegance:

http://example.com/post-name/

Remember to test thoroughly after making any changes! As always, questions & comments welcome. Thanks for reading :)

About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
BBQ Pro: The fastest firewall to protect your WordPress.

19 responses to “Redirect WordPress Date Archives with .htaccess”

  1. Hi, I’ve been trying to get this:
    domain.com/[4-digit year]/[any thing else]
    to redirect to: domain.com/blog/[4-digit year]/[any thing else]

    BUT… I only want to redirect stuff that’s in archives, so I can’t do a blanket redirect to /blog/. Ay ideas?

    Thanks!

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
SAC Pro: Unlimited chats.
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.